babylog_audio_plugin 0.0.2 copy "babylog_audio_plugin: ^0.0.2" to clipboard
babylog_audio_plugin: ^0.0.2 copied to clipboard

PlatformAndroidiOS
unlisted

A flutter plugin for streaming audio via websockets using babylog.

example/lib/main.dart

// Import necessary libraries.
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:babylog_audio_plugin/babylog_audio_plugin.dart';
import 'package:babylog_audio_plugin/protobuf/dart/audio_message.pb.dart';

import 'dart:async';

/// Entry point of the application.
void main() {
  runApp(MyApp());
}

/// Main application widget.
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('Audio Stream Plugin Demo')),
        body: AudioStreamDemo(),
      ),
    );
  }
}

/// StatefulWidget representing the Audio Stream demo.
class AudioStreamDemo extends StatefulWidget {
  @override
  _AudioStreamDemoState createState() => _AudioStreamDemoState();
}

/// State class for the [AudioStreamDemo] widget.
class _AudioStreamDemoState extends State<AudioStreamDemo> {
  BabylogAudioPlugin? _babylogAudioPlugin;
  bool _isStreaming = false;
  List<AudioSegment> _segments = [];

  /// Initialize the state.
  @override
  void initState() {
    super.initState();
    _babylogAudioPlugin = BabylogAudioPlugin();
    _babylogAudioPlugin?.segmentsStream.listen((segments) {
      setState(() {
        _segments = segments;
      });
    });
  }

  /// Toggle streaming of audio data.
  void _toggleStreaming() async {
    if (!_isStreaming) {
      await _babylogAudioPlugin?.connect('ws://localhost:8080');
    } else {
      await _babylogAudioPlugin?.disconnect();
    }

    setState(() {
      _isStreaming = !_isStreaming;
    });
  }

  /// Send an audio message.
  void _sendAudioMessage() async {
    var audioData = Uint8List.fromList([1, 2, 3, 4]);
    await _babylogAudioPlugin?.sendAudioMessage(audioData, 44100, 1);
    print('Audio message sent.');
  }

  /// Dispose the state.
  void dispose() {
    _babylogAudioPlugin?.disconnect();
    super.dispose();
  }

  /// Build the UI.
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Text(
            'Audio Segments:',
            style: TextStyle(fontWeight: FontWeight.bold),
          ),
          Expanded(
            child: ListView.builder(
              itemCount: _segments.length,
              itemBuilder: (context, index) {
                final segment = _segments[index];
                return ListTile(
                  title: Text('Segment ${index + 1}'),
                  subtitle: Text(
                    'Start time: ${segment.startTime}\n'
                    'End time: ${segment.endTime}\n'
                    'Label: ${segment.label}\n'
                    'Transcription: ${segment.transcription}',
                  ),
                );
              },
            ),
          ),
          ElevatedButton(
            onPressed: _toggleStreaming,
            child: Text(_isStreaming ? 'Stop Streaming' : 'Start Streaming'),
          ),
          ElevatedButton(
            onPressed: _isStreaming ? _sendAudioMessage : null,
            child: Text('Send Audio Message'),
          ),
        ],
      ),
    );
  }
}
1
likes
115
points
64
downloads

Publisher

unverified uploader

Weekly Downloads

A flutter plugin for streaming audio via websockets using babylog.

Homepage

Documentation

API reference

License

GPL-3.0 (license)

Dependencies

flutter, flutter_plugin_android_lifecycle, http, plugin_platform_interface, protobuf, web_socket_channel

More

Packages that depend on babylog_audio_plugin

Packages that implement babylog_audio_plugin