0

I was looking for a way to play music in flutter without pressing a button, but I can't find a way, does someone know how to do that

UPDATE

I've tried using initstate(), but when I implement this, the music would just stop at the middle

this is the current code I have for the initstate()

final player = AudioPlayer();

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance?.addPostFrameCallback((timeStamp) async{
      if(!player.playing) {
        await player.setAsset("assets/mainmusic.mp3");
        await player.setLoopMode(LoopMode.all);
        await player.play();
      }
    });
  }
xpertpert
  • 23
  • 5

1 Answers1

0

If you are using statelesswidget, switch to a stateful widget. Then inside the State put following code.

@override
void initState(){
      'put your music play function'
      super.initState();
}
Bishop03
  • 58
  • 4
  • I did that @Bishop03 but it usually just stops in the middle of the song – xpertpert Aug 12 '22 at 16:44
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 17 '22 at 15:05