简体   繁体   English

在 Flutter 中播放音频文件

[英]Play a audio File in Flutter

hey i want for my app background music, that for I need to know, how to play a Audio File in Flutter, i already imported many Adio packages but no one really worked, i am new in the flutter game xD,if it's possible it should be short command and i need it for an android App.I hope someone can help me,thanks for your help.嘿,我想要我的应用背景音乐,因为我需要知道如何在 Flutter 中播放音频文件,我已经导入了许多 Adio 包,但没有一个真正有效,我是 flutter 游戏 xD 的新手,如果可能的话应该是简短的命令,我需要它用于 android 应用程序。我希望有人能帮助我,谢谢你的帮助。

nearly worked差点工作

I've been in the same situation as you.我和你的情况一样。 And audio is hard(so be prepared).音频很难(所以要做好准备)。 I did get my background-music to work after ^4 days of work (so sorry for a long answer, but that's how to gotta be).在 ^4 天的工作后,我确实让我的背景音乐开始工作(很抱歉回答太长,但这就是必须的)。 Hoppfully your's dosn't take that long:很高兴你的不需要那么长时间:

First you need to add Audio Players to the app:首先,您需要将音频播放器添加到应用程序:

import 'package:audioplayers/audio_cache.dart';
import 'package:audioplayers/audioplayers.dart';

import these in the page the audio is being played on (I recommend playing the background music from the main page (main.dart))在播放音频的页面中导入这些内容(我建议从主页(main.dart)播放背景音乐)

Then under the Class state you need to make the audio-player and audio-cache , as so:然后在Class 状态下,您需要制作音频播放器音频缓存,如下所示:

class _YourMainPageNameState extends State<YourMainPageName> {

AudioPlayer player = AudioPlayer();  //add this
  AudioCache cache = new AudioCache();  //and this

@override
  Widget build [...]

Then whenever you want to play the audio, call然后每当你想播放音频时,调用

player = await cache.loop('assets/yourMusicFileHere');

And to stop it call:并阻止它调用:

player.stop();

If you want the music to start from the point that the app opens, wrap the Scaffold with a WillPopScope like so:如果您希望音乐从应用程序打开的那一点开始,请使用WillPopScope包裹Scaffold如下所示:

Future<bool> _willPopCallback() async {
    player = await cache.loop('assets/yourMusicFileHere');
    return true;
  }

override
  Widget build(BuildContext context) {
return WillPopScope(onWillPop: _willPopCallback, child: new Scaffold( [...] )
}

Feel free to send me any errors you got, I'll be on stackOverFlow multible times a day, so I'mma answer.请随时向我发送您遇到的任何错误,我将每天多次访问 stackOverFlow,所以我会回答。 I think I got everything in this comment, but somthing might be missing, so make sure to hit me up with that ;)我想我在此评论中得到了所有内容,但可能缺少某些内容,因此请务必与我联系 ;)



Edit: I just realised that I made a mistake, I sayd that WillPopScope could be used to start the music when the app started, that's not true, WillPopScope is for when you leave the page.编辑:我刚刚意识到我犯了一个错误,我说应用程序启动时可以使用WillPopScope开始播放音乐,这不是真的, WillPopScope是在您离开页面时使用的。 So it can be used to stop music when the user leaves the app.因此,它可用于在用户离开应用程序时停止音乐。

Now for the music to start when the app is opened, we'll have to call a function when the widget-tree is being build like so:现在为了在应用程序打开时播放音乐,我们必须在构建小部件树时调用一个函数,如下所示:

Future<bool> _willPopCallback() async { 
    player.stop(); //change this
    return true;
  }

openingActions() async { //add this
    player = await cache.loop('assets/yourMusicFileHere.mp3'); //add this
  } //add this

  @override
  Widget build(BuildContext context) {
    openingActions();
    return WillPopScope(onWillPop: _willPopCallback, child: new Scaffold( [...] )

You might run into issues where the music is being played twice, then you need to add a security-check when you play the music, like this:您可能会遇到音乐播放两次的问题,然后您需要在播放音乐时添加安全检查,如下所示:

 bool isPlaying = false;

 Future<bool> _willPopCallback() async { //change this whole thing
    if(isPlaying == false) {
      setState(() { 
        isPlaying = true;
      });
      player.stop();
    }
   return true;
  }
  • Thanks - Tobias谢谢 - 托比亚斯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM