Flutter音频处理插件flutter_sound_suite的使用
Flutter音频处理插件flutter_sound_suite的使用
flutter_sound_suite
是一个用于轻松记录和播放声音的 Flutter 插件!
依赖项
以下是 flutter_sound_suite
所需的依赖项:
dependencies:
dio: ^4.0.0
intl: ^0.17.0
uuid: ^3.0.4
provider: ^5.0.0
path_provider: ^2.0.2
flutter_sound: ^8.1.4
permission_handler: ^8.0.1
支持的编解码器
flutter_sound
支持以下编解码器:
编码器 | AAC ADS | Opus OGG | Opus CAF | MP3 | Vorbis OGG | PCM raw | PCM WAV | PCM AIFF | PCM CAF | FLAC | AAC MP4 | AMR-NB | AMR-WB |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
iOS | 是 | 是(*) | 是 | 否 | 否 | 否 | 是 | 否 | 否 | 是 | 是 | 否 | 否 |
iOS 解码器 | 是 | 是(*) | 是 | 是 | 否 | 否 | 是 | 是 | 是 | 是 | 是 | 否 | 否 |
Android 编码器 | 是 | 否 | 否 | 否 | 否 | 是 | 是 | 否 | 否 | 否 | 是 | 是 | 是 |
Android 解码器 | 是 | 是 | 是(*) | 是 | 是 | 是 | 是 | 是(*) | 是(*) | 是 | 是 | 是 | 是 |
(*):虽然 flutter_sound
支持这些编解码器,但需要文件格式转换。这可能会带来一些问题:
- 需要 FFmpeg。FFmpeg 不包含在
flutter_sound
的轻量版本中。 - 播放文件时或停止录音后可能会有一些延迟,对于非常大的录音文件,这种延迟可能相当明显。
此表格将在添加更多编解码器时进行更新。
完整示例
以下是一个完整的示例,展示了如何使用 flutter_sound_suite
进行录音和播放音频。
import 'package:flutter/material.dart';
import 'package:flutter_sound_suite/flutter_sound_suite.dart';
import 'package:provider/provider.dart';
void main() {
runApp(FlutterSoundSuiteApp());
}
class FlutterSoundSuiteApp extends StatefulWidget {
@override
_FlutterSoundSuiteAppState createState() => _FlutterSoundSuiteAppState();
}
class _FlutterSoundSuiteAppState extends State<FlutterSoundSuiteApp> {
List<Map<String, dynamic>> data = [];
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Sound Suite Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Sound Suite Demo'),
),
body: ChangeNotifierProvider(
create: (context) => SoundPlayerModel(),
child: Container(
child: ListView.builder(
itemBuilder: (BuildContext context, int index) {
Map<String, dynamic> item = data[index];
return Card(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(item['fileName']),
Text('duration ${item['duration']}'),
Card(
color: Colors.red.shade50,
child: SoundPlayerWidget(
filePath: item['filePath'],
fileName: item['fileName'],
duration: item['duration'],
),
),
],
),
);
},
itemCount: data.length,
),
),
),
bottomNavigationBar: SafeArea(
child: Container(
height: 70,
child: SoundRecorderWidget(
recordComplete: (
String filePath,
String fileName,
double duration,
) {
setState(() {
data.add({
'filePath': filePath,
'fileName': fileName,
'duration': duration,
});
debugPrint('保存到 $filePath,时长 $duration');
});
},
),
),
),
),
);
}
}
代码解释
-
导入必要的包:
import 'package:flutter/material.dart'; import 'package:flutter_sound_suite/flutter_sound_suite.dart'; import 'package:provider/provider.dart';
-
定义主应用类:
void main() { runApp(FlutterSoundSuiteApp()); } class FlutterSoundSuiteApp extends StatefulWidget { @override _FlutterSoundSuiteAppState createState() => _FlutterSoundSuiteAppState(); }
-
定义状态类:
class _FlutterSoundSuiteAppState extends State<FlutterSoundSuiteApp> { List<Map<String, dynamic>> data = []; @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Sound Suite Demo', theme: ThemeData( primarySwatch: Colors.blue, visualDensity: VisualDensity.adaptivePlatformDensity, ), home: Scaffold( appBar: AppBar( title: Text('Flutter Sound Suite Demo'), ), body: ChangeNotifierProvider( create: (context) => SoundPlayerModel(), child: Container( child: ListView.builder( itemBuilder: (BuildContext context, int index) { Map<String, dynamic> item = data[index]; return Card( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(item['fileName']), Text('duration ${item['duration']}'), Card( color: Colors.red.shade50, child: SoundPlayerWidget( filePath: item['filePath'], fileName: item['fileName'], duration: item['duration'], ), ), ], ), ); }, itemCount: data.length, ), ), ), bottomNavigationBar: SafeArea( child: Container( height: 70, child: SoundRecorderWidget( recordComplete: ( String filePath, String fileName, double duration, ) { setState(() { data.add({ 'filePath': filePath, 'fileName': fileName, 'duration': duration, }); debugPrint('保存到 $filePath,时长 $duration'); }); }, ), ), ), ), ); } }
更多关于Flutter音频处理插件flutter_sound_suite的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复