flutter - 停止音频循环(音频播放器包)

标签 flutter dart

我是 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/

相关文章:

flutter - 不接受7个SDK软件包许可证中的6个。 100%计算更新

flutter - 如何在Flutter中将相同的BLoC用于不同的小部件?

async-await - Flutter - 导航器回调刷新屏幕中的异步方法

flutter - 如何在2种颜色的 flutter 中创建Flatbutton?不渐变,并排2种纯色

dart - 如何使用 Dart 的 HttpClient 上传 PDF?

testing - 在 Flutter 的 pumpAndSettle() 中,如何在不传入 EnginePhase 的情况下指定超时?

flutter - 如何从目录导入所有 Dart 文件?

Flutter 如何在 ListTile 上获取弹出菜单?

android - 如何在Android屏幕上显示HTTP请求的异常

Flutter: float 操作按钮固定位置