MediaPlayer.IsMuted set to false on Android resume

Hey guys,

Encountered a strange issue in my game and was able to easily reproduce it in a new project. If I have audio playing but MediaPlayer.IsMuted is set to true, then hit the power button on my phone (or emulator) to pause, then hit it again to bring it back, MediaPlayer.IsMuted gets set to false and audio can be heard.

I’ve logged this as an issue here:

I thought I would be able to work around this by trapping the Android OnPause and OnResume/OnPostResume events to save and restore state; however, it looks like sometimes the resume event gets fired too early and MediaPlayer automatically sets itself to unmuted again. The best I’ve been able to do is stick a conditional in my Update method that constantly ensures that MediaPlayer.IsMuted is equal to my stored value and, if it is not, sets it. This leads to hearing a blip of audio when MonoGame unmutes it but before the Update can set things right.

I know there’s folks around here who are working on, and have released titles… has anybody encountered this before? If so, is there a better way to work around it?

The bug is still there, but I’ve changed my approach. I’ve prototyped it and it seems to work. Before, when I disabled my audio, I’d just mute it and let it continue to play in the background. Now I’m actually stopping the music. I also tie into OnPause and OnPostResume of AndroidGameActivity to suspend/resume playback appropriately.

Initial tests seem to show it working on. I’ve gotta do some refactoring yet and bring the class into my framework, as well as write a crapton of unit tests, but I think I’m ok.

I’m happy to share the prototype… but I’m not entirely sure how to upload a zip file. Here’s a pastebin of the controller and player classes though.

MusicPlayer - https://pastebin.com/4aZQG4En
MusicController - https://pastebin.com/SHUfAPet