Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter (Dart): Get/Record audio stream from microphone and play it back immediately (real-time)

I need to be able to capture a stream of audio from the microphone and then pass it as argument or read it immediately in order to play it back as audio. To implement this in any other framework there are excellent tools and functions you can use but I need to archive that functionality on Flutter.

Any help or suggestions?

like image 288
oetoni Avatar asked Sep 25 '18 14:09

oetoni


1 Answers

Please try this package flutter_sound.
https://github.com/dooboolab/flutter_sound
Here is reference link
https://medium.com/flutterpub/flutter-sound-plugin-audio-recorder-player-e5a455a8beaf

Creating instance.

FlutterSound flutterSound = new FlutterSound();

Starting recorder with listener.

String path = await flutterSound.startRecorder(null);
print('startRecorder: $path');

_recorderSubscription = flutterSound.onRecorderStateChanged.listen((e) {
  DateTime date = new DateTime.fromMillisecondsSinceEpoch(e.currentPosition.toInt());
  String txt = DateFormat('mm:ss:SS', 'en_US').format(date);
});

Stop recorder

String result = await flutterSound.stopRecorder();
print('stopRecorder: $result');

if (_recorderSubscription != null) {
    _recorderSubscription.cancel();
    _recorderSubscription = null;
}

Start player

String path = await flutterSound.startPlayer(null);
print('startPlayer: $path');

_playerSubscription = flutterSound.onPlayerStateChanged.listen((e) {
    if (e != null) {
        DateTime date = new DateTime.fromMillisecondsSinceEpoch(e.currentPosition.toInt());
        String txt = DateFormat('mm:ss:SS', 'en_US').format(date);
        this.setState(() {
            this._isPlaying = true;
            this._playerTxt = txt.substring(0, 8);
        });
    }
});
like image 155
chunhunghan Avatar answered Nov 05 '22 19:11

chunhunghan