我是 Flutter 编程的新手,我想创建一个应用程序,我需要一个音频文件在后台播放/循环播放。但是,当双击屏幕时它应该会停止。
音频保存在 Assets 文件夹中。我可以播放它,但我不知道如何暂停/停止它。我正在使用这个 package .
@override
Widget build(BuildContext context) {
audioCache.play('rainsound.mp3', );
return new Scaffold(
child: new GestureDetector(
onDoubleTap: () {
//here I would like to stop the audio
debugPrint('audio stopped');
},
最佳答案
更新(audioplayers v1.0.1
)
播放:
final player = AudioPlayer();
// Assuming you have the file in "assets/audio/my_audio.mp3"
player.play(AssetSource('audio/my_audio.mp3'));
停止:
audioPlayer.stop();
您必须获取 AudioPlayer
的实例才能停止播放文件,只需在 play()
上使用 await 获取实例并使用它,您可以调用 停止()
。这是工作代码。
AudioCache cache; // you have this
AudioPlayer player; // create this
void _playFile() async{
player = await cache.play('my_audio.mp3'); // assign player here
}
void _stopFile() {
player?.stop(); // stop the file like this
}
关于flutter - 停止音频循环(音频播放器包),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56360083/