停止音频循环(audioplayers程序包) [英] Stop audio loop (audioplayers package)

查看:347
本文介绍了停止音频循环(audioplayers程序包)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to flutter programming and I want to create an application, where I need an audio file to play/loop in the background. However it should stop, when double tapping on the screen.

音频保存在资产文件夹中。我可以播放它,但是我不知道如何暂停/停止它。我正在使用此

The audio is saved in the assets folder. I am able to play it, but i don't know how to pause/stop it. I am using this 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');
        },


推荐答案

要获取 AudioPlayer 的实例来停止文件,只需在 play()上使用await即可获取该实例并使用,您可以调用 stop()。这是工作代码。

You will have to get the instance of AudioPlayer to stop the file, simply use await on play() to get the instance and using this, you can call stop(). This is the working code.

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
}

这篇关于停止音频循环(audioplayers程序包)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆