florent37 / Flutter-AssetsAudioPlayer

Play simultaneously music/audio from assets/network/file directly from Flutter, compatible with android / ios / web / macos, displays notifications
https://pub.dartlang.org/packages/assets_audio_player
Apache License 2.0
755 stars 360 forks source link

android: foreground -> background -> switch next song -> foreground will be played again #689

Open zhushenwudi opened 2 years ago

zhushenwudi commented 2 years ago

Flutter Version

My version : 2.10.4

Lib Version

My version : 3.0.4+1

Platform (Android / iOS / web) + version

Platform : android

Describe the bug

Switch to the background when a song is playing, switch to the next song, return to the foreground, and the song will be played again

zhushenwudi commented 2 years ago

This is not a bug, it is only an issue by business, if repository owner read my issue, consider whether to add to these code.

I add these code in "assets_audio_player.dart" about 310 lines.

final BehaviorSubject<bool> _isFirstBackgroundToForeground = BehaviorSubject<bool>.seeded(false);
bool get isFirstBackgroundToForeground {
  final isFirst = _isFirstBackgroundToForeground.stream.value;
  _isFirstBackgroundToForeground.value = false;
  return isFirst;
}

And then I add these code at AppLifecycleObserver callback in "assets_audio_player.dart".

onChanged: (isActive) {
  _isFirstBackgroundToForeground.value = isActive;
}

At last, can use at main code, it can not broken player logic because you can not set value.

mPlayer.isFirstBackgroundToForeground