MediaPlayer.Play() freezing game

I noticed that the first time I play a song using MediaPlayer.Play(), it takes one tenth of a second or two before the song actually starts. The following times, instead, it takes a few thousandths of a second. I read the topic MediaPlayer.Play() makes the game lag but it seems it doesn’t have a good solution. Any suggestion to avoid the initial delay?

File type?

mp3

Try .ogg

Tried .ogg. Same result…

What bitrates?

Hey, this sounds similar to an issue I worked through a couple of weeks ago. My game would switch songs (and call MediaPlayer.Play()) depending on what was happening in the game. Everytime this happened the game would freeze for a moment. It was particularly noticeable and gamebreaking on android.

I looked around and found several related posts but no clear answer. What finally worked for me was loading the song as a SoundEffect and then using a SoundEffectInstance instead of using the MediaPlayer. Something more like this:

goodSongSoundEffect = cm.Load<SoundEffect>("goodSong");
var goodSongSoundEffectInstance = goodSongSoundEffect.CreateInstance();
goodSongSoundEffectInstance.Play();

This walkthrough was helpful as well http://rbwhitaker.wikidot.com/playing-sound-effects

My understanding now is that MediaPlayer should really only be used in places where a momentary pause won’t be noticed (i.e. loading screen). Hope this helps.

1 Like

291 kbps

What I didn’t know is that you can load an mp3 file as a SoundEffect, but reading Playing Sound Effects - RB Whitaker's Wiki I found out how to do it, and your suggestion works perfectly. Thanks so much! :slight_smile:

The problem with using this system is that the resulting xnb file is much larger than the corresponding mp3 file. Have you noticed it?

SoundEffectInstance.Play() would also cause some lag in case of playing sound with OpenAL, which is blocking. You can put the play call in worker thread, but you have to wait 3.8 release. There’s a race condition issue around it.