停止音频循环(audioplayers程序包)

时间:2019-05-29 11:57:11

标签: flutter dart

我是Flutter编程的新手,我想创建一个应用程序,我需要一个音频文件来在后台播放/循环。但是,在屏幕上双击时,它应该停止。

音频被保存在资产文件夹中。我可以播放它,但是我不知道如何暂停/停止它。我正在使用此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');
        },

1 个答案:

答案 0 :(得分:2)

您将必须获取AudioPlayer的实例才能停止文件,只需在play()上使用await即可获取该实例,然后使用它,可以调用stop()。这是工作代码。

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
}
相关问题