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

oet*_*oni 7 audio real-time microphone dart flutter

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

任何帮助或建议?

chu*_*han 4

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

创建实例。

FlutterSound flutterSound = new FlutterSound();
Run Code Online (Sandbox Code Playgroud)

用监听器启动录音机。

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);
});
Run Code Online (Sandbox Code Playgroud)

停止录音机

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

if (_recorderSubscription != null) {
    _recorderSubscription.cancel();
    _recorderSubscription = null;
}
Run Code Online (Sandbox Code Playgroud)

启动播放器

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);
        });
    }
});
Run Code Online (Sandbox Code Playgroud)