English | 简体中文
Flutter 录音插件 支持录音/暂停/继续/停止, 可以在录音的同时获取到底层提供的音频信息(如声音强度).
加入 flutter_audio_recorder
到你的 pubspec.yaml
- 修改plist, 加入下面这一条
<key>NSMicrophoneUsageDescription</key>
<string>Can We Use Your Microphone Please</string>
- 然后在页面需要录音功能的时候调用
hasPermission
API
- 加
uses-permission
到./android/app/src/main/AndroidManifest.xml
,跟平级, 像下面这样
...
</application>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
...
</manifest>
- 然后在页面需要录音功能的时候调用
hasPermission
API
- 开启AndroidX的项目: 请使用最新版本 (
0.5.x
) - 未使用AndroidX的项目: 可以使用旧版本 (
0.4.9
)
建议使用方式: hasPermission
=> init
> start
-> (pause
<-> resume
) * n -> stop
), 重新开始新录音的话 流程一样
bool hasPermission = await FlutterAudioRecorder.hasPermissions;
var recorder = FlutterAudioRecorder("filename.mp4"); // .wav .aac .m4a
await recorder.initialized;
或者
var recorder = FlutterAudioRecorder("file_path", audioFormat: AudioFormat.AAC); // or AudioFormat.WAV
await recorder.initialized;
var recorder = FlutterAudioRecorder("file_path", audioFormat: AudioFormat.AAC, sampleRate: 22000); // 采样率默认值 16000
await recorder.initialized;
Audio Format | Audio Extension List |
---|---|
AAC | .m4a .aac .mp4 |
WAV | .wav |
await recorder.start();
var recording = await recorder.current(channel: 0);
var current = await recording.current(channel: 0);
// print(current.status);
设置一个Timer,定期获取信息(录音结束后,记得cancel
)
new Timer.periodic(tick, (Timer t) async {
var current = await recording.current(channel: 0);
// print(current.status);
setState(() {
});
});
Name | Description |
---|---|
path | String |
extension | String |
duration | Duration |
audioFormat | AudioFormat |
metering | AudioMetering |
status | RecordingStatus |
Name | Description |
---|---|
peakPower | double, 强度极值 |
averagePower | double, 强度平均值 |
isMeteringEnabled | bool, 是否启用(True) |
Unset
,Initialized
,Recording
,Paused
,Stopped
await recorder.pause();
await recorder.resume();
var result = await recorder.stop();
File file = widget.localFileSystem.file(result.path);
用Xcode打开Example项目可以查看示例
This project is a starting point for a Flutter plug-in package, a specialized package that includes platform-specific implementation code for Android and/or iOS.
For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.