我下面有一段代码,每次按下按钮都会播放一个声音。
AudioPlayer player = AudioPlayer();
Future<void> playSound({required String soundStr}) async {
player.setAsset(soundStr);
player.play();
}
但每当我快速按下按钮时,有时它就会显示这个错误。如何消除此错误?我尝试添加player.stop();
,但它仍然是一样的。
E/flutter ( 4312): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(abort, Loading interrupted, null, null)
E/flutter ( 4312): #0 AudioPlayer._setPlatformActive.checkInterruption (package:just_audio/just_audio.dart:1236:7)
E/flutter ( 4312): #1 AudioPlayer._setPlatformActive.setPlatform (package:just_audio/just_audio.dart:1347:11)
E/flutter ( 4312): <asynchronous suspension>
E/flutter ( 4312):
E/flutter ( 4312): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(abort, Loading interrupted, null, null)
E/flutter ( 4312):
编辑:我尝试添加await
。
Future<void> playSound({required String soundStr}) async {
await player.setAsset(soundStr);
await player.play();
}
如果我快速按下按钮,它仍然会有错误。尽管现在不同了。
E/flutter (21654): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Connection aborted
E/flutter (21654): #0 AudioPlayer._load (package:just_audio/just_audio.dart:843:11)
E/flutter (21654): <asynchronous suspension>
E/flutter (21654): #1 AudioPlayer.load (package:just_audio/just_audio.dart:770:14)
E/flutter (21654): <asynchronous suspension>
E/flutter (21654): #2 AudioPlayer.setAudioSource (package:just_audio/just_audio.dart:745:18)
E/flutter (21654): <asynchronous suspension>
E/flutter (21654): #3 playSound (package:super_pinoy_quiz/common/generic_methods.dart:396:3)
E/flutter (21654): <asynchronous suspension>
E/flutter (21654):
我正在使用此程序包:just_audio:^0.9.29虽然我尝试使用不同的软件包,但在快速按下按钮时也遇到了相同的错误:音频播放器:^1.1.1
setAsset
和play
都是未来的方法,您可以等待设置资产
Future<void> playSound({required String soundStr}) async {
await player.setAsset(soundStr);
await player.play();
}