flutter - 停止音频循环(audioplayers 包)
问题描述
我是 Flutter 编程的新手,我想创建一个应用程序,我需要一个音频文件在后台播放/循环。但是,当双击屏幕时,它应该停止。
音频保存在资产文件夹中。我可以播放它,但我不知道如何暂停/停止它。我正在使用这个包。
@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
才能停止文件,只需使用 await onplay()
来获取实例并使用它,您可以调用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
}
推荐阅读
- alfresco - 关闭/禁用自定义文件夹的继承权限
- c# - 访问控制器外部的会话状态
- javascript - Firebase 使用 UID 获取身份验证详细信息
- selenium - 如何根据索引定位 Xpath 的特定节点?
- chef-infra - Chef - Mount - 如何添加持久性 (/p:yes)
- angular - 从角度 2 迁移到角度 6 后在运行时未检测到新更改
- mongodb - 无法创建对具有 NULL id mongo hibernate-mongo 和 spring boot 的对象的引用
- selenium - 验证所有验证消息
- django - django allauth 重定向问题调试
- javascript - 如何摆脱 `no-unused-expressions` eslint 错误,用于`&&`和`?:`用法?