MediaPlayer.Play() makes the game lag

I’ve noticed that whenever I’m calling MediaPlayer.Play(), I lose a few frames in the following second. So I’ve tried putting the Play() call inside a thread but it doesn’t seems like it solved my issue, since it’s still very laggy. I was wondering if this was a known performance issue or if I did something wrong, or if there’s something else I can do to prevent frame loss. Or really any advice regarding music management on android.

I haven’t experienced this. I went and dug through some old code to make sure that I am using MediaPlayer.Play(…) and indeed, I am. I’m not sure what’s different.

A couple of questions…

  1. Does this happen every time you play a song, or only the first time?
  2. Where does this happen? By your thread tag, it’s on Android, but is it on the simulator or on a physical device?
  1. It happens every time I call MediaPlayer.Play(song), whether I specify a TimeSpan or not. In my game, I have to call this function frequently because the music change depending on the player state and location.
  2. I build directly on my device, and it happens even when I’m not debugging.

Hmm, that’s odd… and definitely sounds frustrating! The only other thing I might suggest would be to try making a stand alone project to see if you can reproduce the issue there.

So I just tried on another almost empty android project of mine, and indeed, when calling Play(), the game freezes a bit. Less than in my game but that’s probably due to the fact that the game updates a lot of thing so the impact is bigger.

I also tried switching the format of the file from wav to mp3 but couldn’t notice any difference.

So when calling MediaPlayer.Play(song), you don’t have any slowdown, even for a very brief period of time? If that’s the case, it is definitely odd. I should try on another device.

I definitely did not! I haven’t updated my dev environment in a good while, but tomorrow I’ll try to take some time and see if I can see the same issue you are.

Does it lag in a windows project?

From what I remember I didn’t have any problem with windows projects, no. I’ll take the time to double check just in case.

Thanks for taking the time to answer.

Hey, in the mean time, I did find this…

It looks like you might not be the only one to experience a lag here. Their suggestion seems to play, then pause, during your level load so that when you want to play it for real, it’s ready to go. Or just use another music provider :slight_smile:

Monogame (content) exports songs as aac (no matter what you provide) - is this still true? Because the android mediaplayer - for whatever reason - has a lag with that sort of format. Check out if that’s the case. As a test you could just supply the wav as file and load it at runtime (not from content pipeline)

You could also try to use SoundEffect instead - if it’s changing frequently, that may be an option

Thanks, I apparently missed this post during my research. But this trick didn’t worked for me, both by calling the “play then pause” during LoadContent() and at some point point in Update(). I’ll look into other music providers, hopefully I get one to work.

Thanks, I didn’t know about aac, I’ll check and make the test you suggest.

About SoundEffect : I’m not sure I can stop a SoundEffect whenever I want or even playing one on loop. I also need to know at which timespan a song is currently played at, so this solution might not work for me.

Ok, so I loaded up a test project I made a few years years ago (and was still living on my desktop :P). I made a modification to have a rectangle animating across the screen and did a few tests.

Here’s a link to my test project…
https://drive.google.com/file/d/1g4by34rVmTxuHo9VRyUfkl9HPxfe72vF/view?usp=sharing
(I’ll probably remove this after a few days)

There’s a lot in it because it was originally intended to solve a different problem (audio playback resumption after task switching), but what’s important is that it plays music and sound effects. When I start up, both the music and the animation lag a bit. Then, after a few seconds, things settle down and seem normal. After that, I can switch songs and experience no delays at all, in either audio or animation.

I’m not sure if this is related to audio though… if I stop the music from playing on startup (comment out line 61 in Game1.cs), the visuals still lag on startup. Once the visual stop lagging, if I hit the play button, everything sounds fine and there are no subsequent lags.

I think what I’m seeing here is just some initial startup hitches, which is easily mitigated by a splash screen and probably why I didn’t notice it in the actual game I’m developing. This sounds like it’s different for you, where you’re experiencing a lag on every call of MediaPlayer.Play.

Maybe give this project a try. Presumably, for you, every time you hit the play button, or change songs, it will lag for you even though it doesn’t for me?

Thank you for this test project, I was able to build it on my device and noticed that indeed, the screen freezes for a short period of time when changing the music, even after a bit of time. This doesn’t happen with sound effects and doesn’t happen when the music is disabled either, so that’s really an issue with MediaPlayer.Play(). What I should conclude is that this only happen on my device. However I tried building my game on an old android tablet that was laying around and the freeze was still here when changing the music, with less impact but still clearly visible. I’m starting to think this could be an issue only on low end devices, even though my android device is not that bad (nokia 5 on android 9).

Sooo I don’t really know what to do, I guess I could just let it that way and see if it happen on other devices through beta testing, even thought if that’s the case I don’t know what I would do. I looked into other audio alternative but couldn’t find something satisying (XACT seems really outdated).

Hmm, that is interesting.

You mentioned that your test devices are lower end. I’m curious, how does it behave on your simulator running on your dev box?

Unfortunately, my PC isn’t powerful enough to run an emulator, that’s why I only build directly on device (if that was your question).

It does answer my question :slight_smile:

The project I sent you also worked fine without hitching when I tested it (a few years ago) on a Samsung Galaxy S8, which is at least 3 years old by now. So I think you’re probably safe for end users. Having said that, I think it definitely makes sense to look into another audio solution if you want your product to be available to the widest user base possible.

It’s really hard to say if the problem is MG, or this just normal behaviour. It seems odd that it would be normal.

1 Like