【发布时间】:2019-05-26 18:45:27
【问题描述】:
我正在尝试在 Flutter 中创建一个警报,在该警报音应该在一定时间后响起。在 Flutter 中,这似乎说起来容易做起来难!
尝试使用音频播放器插件来实现这一点。使用了 playLocal 函数,其中资产从 rootbundle 加载到应用程序目录中,然后播放
根据 audioplayer github repo 中的答案,这是应该可以解决问题的代码:
class SoundManager {
AudioPlayer audioPlayer = new AudioPlayer();
Future playLocal(localFileName) async {
final dir = await getApplicationDocumentsDirectory();
final file = new File("${dir.path}/$localFileName");
if (!(await file.exists())) {
final soundData = await rootBundle.load("assets/$localFileName");
final bytes = soundData.buffer.asUint8List();
await file.writeAsBytes(bytes, flush: true);
}
await audioPlayer.play(file.path, isLocal: true);
}
}
我不断收到错误消息:“无法加载资产”。资产(mp3/wav 文件)显然在文件夹中,并且该文件夹正确包含在 pubspec.yaml 文件中(其他图像资产正在从该文件夹正确加载,因此指定文件夹本身不是这里的问题)
【问题讨论】:
-
是的,这是我上面的问题中引用的那个线程的答案 (github.com/rxlabz/audioplayer/issues/5#issuecomment-388699508)