sound_stream 0.2.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 86

sound_stream #

This plugin is still in early development stage

A Flutter plugin started from my own needs: Stream audio data from Mic and data to Audio engine without using a file. We can use it to stream audio via network or use it with STT/TTS functions.

Current features:

  • Provides stream of data from mic (Uint8List)
  • Player that receive stream of raw sound data (Uint8List)
  • Support both Android and iOS (cross-platform)
  • Recorder & Player can work simultaneously

Limitations:

To-do list:

  • Support more audio formats
  • Support more platforms (Windows, macOS, Web)
  • Current code might be messy. Should clean it up (someday)

0.2.0 #

  • Recorder & Player can work simultaneously
  • Minor code refactor

0.1.0+1 #

Refactor to match pub.dev's Health suggestions

  • Add a dispose method

0.1.0 #

Cross-platform support

  • Audio data can be sent from Android to iOS and vice versa.
  • Player that receive stream of PCM 16bit sound data in a (Uint8List).
  • Added new example: Send data via WebSocket
  • Fixed some bugs.

0.0.1 #

Initial release

  • Provides stream of data from mic (Uint8List).
  • Player that receive stream of raw sound data (Uint8List).
  • Support both Android and iOS.

example/lib/main.dart

import 'dart:async';
import 'dart:typed_data';
import 'package:flutter/material.dart';

import 'package:sound_stream/sound_stream.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  RecorderStream _recorder = RecorderStream();
  PlayerStream _player = PlayerStream();

  List<Uint8List> _micChunks = [];
  bool _isRecording = false;
  bool _isPlaying = false;

  StreamSubscription _recorderStatus;
  StreamSubscription _playerStatus;
  StreamSubscription _audioStream;

  @override
  void initState() {
    super.initState();
    initPlugin();
  }

  @override
  void dispose() {
    _recorderStatus?.cancel();
    _playerStatus?.cancel();
    _audioStream?.cancel();
    super.dispose();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlugin() async {
    _recorderStatus = _recorder.status.listen((status) {
      if (mounted)
        setState(() {
          _isRecording = status == SoundStreamStatus.Playing;
        });
    });

    _audioStream = _recorder.audioStream.listen((data) {
      if (_isPlaying) {
        _player.writeChunk(data);
      } else {
        _micChunks.add(data);
      }
    });

    _playerStatus = _player.status.listen((status) {
      if (mounted)
        setState(() {
          _isPlaying = status == SoundStreamStatus.Playing;
        });
    });

    await Future.wait([
      _recorder.initialize(),
      _player.initialize(),
    ]);
  }

  void _play() async {
    await _player.start();

    if (_micChunks.isNotEmpty) {
      for (var chunk in _micChunks) {
        await _player.writeChunk(chunk);
      }
      _micChunks.clear();
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            IconButton(
              iconSize: 96.0,
              icon: Icon(_isRecording ? Icons.mic_off : Icons.mic),
              onPressed: _isRecording ? _recorder.stop : _recorder.start,
            ),
            IconButton(
              iconSize: 96.0,
              icon: Icon(_isPlaying ? Icons.pause : Icons.play_arrow),
              onPressed: _isPlaying ? _player.stop : _play,
            ),
          ],
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  sound_stream: ^0.2.0

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:sound_stream/sound_stream.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
73
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
86
Learn more about scoring.

We analyzed this package on Jul 11, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform linux

Because:

  • package:sound_stream/sound_stream.dart that declares support for platforms: android, ios

Package does not support Flutter platform macos

Because:

  • package:sound_stream/sound_stream.dart that declares support for platforms: android, ios

Package does not support Flutter platform web

Because:

  • package:sound_stream/sound_stream.dart that declares support for platforms: android, ios

Package does not support Flutter platform windows

Because:

  • package:sound_stream/sound_stream.dart that declares support for platforms: android, ios

Package not compatible with SDK dart

Because:

  • sound_stream that is a package requiring null.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.3.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.2
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
Dev dependencies
flutter_test