Continu my music after a pause in just_audio plugin

Viewed 371

I use the plugin just_audio How can I continu my music after to do a pause? please.

void _play() {
    audioPlayer.setAsset("assets/applause.mp3");
    audioPlayer.play();
  }
void _pause() async {
    await audioPlayer.pause();
  }
  void _rePlay() async {
    ?????????????????????????
  }
2 Answers

You can just call audioPlayer.play(). This works exactly like the play button in a music app, while pause() works exactly like the pause button in a music app.

It is designed this way so that you can simply wire up your play and pause buttons directly to these player methods.

For example, here are the play/pause buttons from the just_audio example app:

// Play button:
IconButton(
  icon: Icon(Icons.play_arrow),
  onPressed: audioPlayer.play,
)
// Pause button:
IconButton(
  icon: Icon(Icons.pause),
  onPressed: audioPlayer.pause,
)

You can just alternate calls to play()/pause()/play()/pause(). Note the left side of this state diagram from the README:

enter image description here

In general, after calling pause, call play to "resume".

In your case, you should NOT set the asset file again. This resets the process.

Below is your code, pasted from your question:

void _play() { // BAD EXAMPLE
  audioPlayer.setAsset("assets/applause.mp3"); // <-- PROBLEM
  audioPlayer.play();
}

You should move setAsset call elsewhere, like in init state.

Related