本文介绍了如何在Flutter中使用Audioplayer插件播放本地mp3文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Flutter中使用 audioplayer 0.2.0 播放本地mp3文件.
How to play local mp3 file with audioplayer 0.2.0 in Flutter.
pubspec.yaml
pubspec.yaml
flutter:
assets:
- sounds/music.mp3
main.dart
main.dart
Future<ByteData> loadAsset() async {
return await rootBundle.load('sounds/music.mp3');
}
// FIXME: This code is not working.
Future playLocal() async {
final result = await audioPlayer.play(loadAsset());
if (result == 1) setState(() => playerState = PlayerState.playing);
}
我可以从rootBundle获取本地资产路径吗?我可以在audioPlayer上从ByteData支付音频吗?
Can I get local assets path from rootBundle?Can I pay audio from ByteData on audioPlayer?
推荐答案
audioplayer
插件当前仅支持网络路径和文件.您可以将资产移动到临时文件夹并使用以下代码播放它:
The audioplayer
plugin currently only supports network paths and files. You can move an asset to a temporary folder and play it with this code:
import 'package:path_provider/path_provider.dart';
...
final file = new File('${(await getTemporaryDirectory()).path}/music.mp3');
await file.writeAsBytes((await loadAsset()).buffer.asUint8List());
final result = await audioPlayer.play(file.path, isLocal: true);
这篇关于如何在Flutter中使用Audioplayer插件播放本地mp3文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!