flutter_sound
Flutter 插件,用于声音。音频录制器和播放器。

该插件为 android 和 ios 平台提供了简单的录音和播放功能。此插件仅支持每个平台的默认文件扩展名。此插件处理来自远程 URL 的文件。此插件可以处理来自本地的播放流(以精确同步桥接时间)。

安装
在 pubspec.yaml 中添加 flutter_sound 作为依赖项
有关如何添加依赖项的帮助,请查看 文档。
安装后
在 iOS 上,您需要在 info.plist 中添加使用说明
<key>NSMicrophoneUsageDescription</key>
<string>This sample uses the microphone to record your speech and convert it to text.</string>
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>
在 Android 上,您需要在 AndroidManifest.xml 中添加权限
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
方法
| 功能 | 参数 | 返回 | 描述 |
|---|---|---|---|
| setSubscriptionDuration | double 秒 |
String 消息 |
设置订阅计时器(以秒为单位)。如果未调用此方法,则默认为 0.01。 |
| startRecorder | String uri |
String uri |
开始录音。这将返回使用的 uri。 |
| stopRecorder | String 消息 |
停止录音。 | |
| startPlayer | String uri |
String 消息 |
开始播放。 |
| stopPlayer | String 消息 |
停止播放。 | |
| pausePlayer | String 消息 |
暂停播放。 | |
| resumePlayer | String 消息 |
恢复播放。 | |
| seekToPlayer | int milliSecs 要跳转到的位置 |
String 消息 |
将音频跳转到选定的位置(以秒为单位)。参数应小于音频时长才能正确放置。 |
订阅
| 订阅 | 返回 | 描述 |
|---|---|---|
| onRecorderStateChanged | <RecordStatus> |
录音开始时可以收听订阅。 |
| onPlayerStateChanged | <PlayStatus> |
播放器开始时可以收听订阅。 |
默认 uri 路径
当在 startRecorder 或 startPlayer 中未设置 uri 路径(在 函数调用 时),它们将根据平台保存在以下路径中。
- Android 的默认路径
sdcard/sound.mp4.
- iOS 的默认路径
sound.m4a.
用法
创建实例。
FlutterSound flutterSound = new FlutterSound();
开始录音,并带有监听器。
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);
});
}
});
停止录音
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);
});
}
});
停止播放
String result = await flutterSound.stopPlayer();
print('stopPlayer: $result');
if (_playerSubscription != null) {
_playerSubscription.cancel();
_playerSubscription = null;
}
暂停播放
String result = await flutterSound.pausePlayer();
恢复播放
String result = await flutterSound.resumePlayer();
跳转播放
String result = await flutterSound.seekToPlayer(miliSecs);
设置订阅时长(可选)。未设置时,默认值为 0.01。
/// 0.01 is default
flutterSound.setSubscriptionDuration(0.01);
设置音量。
/// 1.0 is default
/// Currently, volume can be changed when player is running. Try manage this right after player starts.
String path = await flutterSound.startPlayer(null);
await flutterSound.setVolume(0.1);
待办事项
- [ ]
示例项目中的跳转示例 - [x] 音量控制
- [x] 同步录音机回调处理程序的时间