Play a audio File in Flutter

Viewed 5797

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.

nearly worked

1 Answers

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). 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))

Then under the Class state you need to make the audio-player and audio-cache, as so:

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:

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. 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. 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
Related