Flutter(Dart):从麦克风获取/记录音频流并立即播放(实时)

时间:2018-09-25 14:46:39

标签: audio dart flutter real-time microphone

我需要能够从麦克风捕获音频流,然后将其作为参数传递或立即读取,以便将其作为音频播放。要在其他任何框架中实现此功能,您可以使用出色的工具和功能,但我需要将该功能存档在Flutter上。

有什么帮助或建议吗?

1 个答案:

答案 0 :(得分:0)

请尝试使用此程序包flutter_sound。
https://github.com/dooboolab/flutter_sound
这是参考链接
https://medium.com/flutterpub/flutter-sound-plugin-audio-recorder-player-e5a455a8beaf

创建实例。

FlutterSound flutterSound = new FlutterSound();

使用监听器启动记录器。

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);
});

停止记录器

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

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

开始播放器

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);
        });
    }
});