I use audiolayers
and audio_service
plays audio. It works well. When I open other audio applications (such as Apple Music) to play music, my app will stop playing. This is OK. But when I return to my app to query the current playing status, it is still playing.
This is my code:
Container(
height: 60.w,
width: 60.w,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30.w),
gradient: const LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [Color(0xFFD54381), Color(0xFF7644AD)]),
),
child: StreamBuilder<PlaybackState>(
stream: customAudioHandler?.playbackState,
builder: (context, snapshot) {
final playing = snapshot.data?.playing ?? false;
return GestureDetector(
onTap: () => _playOrPause(context, playing),
child: Icon(
playing ? FontIcons.stop : FontIcons.play,
color: Colors.white,
size: 40.w,
),
);
},
),
),
I try to use WidgetsBindingObserver
such:
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.inactive:
break;
case AppLifecycleState.paused:
break;
case AppLifecycleState.resumed:
var audioHandlerState = customAudioHandler?.playbackState.value.playing;
var audioPlayerState = audioPlayer.state;
// print true
debugPrint('didChangeAppLifecycleState-resumed: $audioHandlerState');
// print PlayerState.playing
debugPrint('didChangeAppLifecycleState-resumed: $audioPlayerState');
break;
case AppLifecycleState.detached:
break;
}
super.didChangeAppLifecycleState(state);
}
Their output status is playing
Any help would be appreciated.