views:

86

answers:

1

I've recently added audio to my game loaded under the "Song" class from the XNA framework.

Everything seemed to work fine, until I decided to add the possibility to change the volume of the song, using the MediaPlayer.Volume setter.

However, when I try to change the volume for the first time, it runs really slowly(like 3 to 4 seconds, which is quite a lot for a volume change). After, when I try to change the volume again, the volume changes instantly.

Basically, I have three volume level choices: High, Medium and Low... So my problem is probably not because I change the volume level too often or too quickly.

I'd only like to know if any of you guys know when MediaPlayer.Volume can take more time to run, so I can check where my problem is situated.

+2  A: 

If you Google around you will find there are lots of performance issues with MediaPlayer. And often they depend on the system you are running on. This is due to it using Windows Media Player behind the scenes (on Windows).

Here are a few ways to mitigate the performance issues:

  • Never read values from MediaPlayer (eg: Volume - this includes using += to set it). Store the actual value yourself and only ever assign to it.
  • Due to the above, use the state-change event to receive state change notifications.
  • Place the use of the MediaPlayer class on a separate thread (ThreadPool is sufficient on Windows) - this solves the cases where it simply blocks your main thread (most of them, but on a few systems it can apparently block your whole process or the whole system).
  • The only sure-fire way to make the problems go away is to not use MediaPlayer at all. You can put your music into XACT and compress it (I think you can use SoundEffect as well).

If I had to guess (and I do) - I'd say that moving your volume changes to another thread will solve your problem.

Andrew Russell
Thanks a lot for your answer! I'm definitely changing to XACT instead.
Jesse Emond
Just be aware that this removes the automatic handling of the user's ability to play their own music on the Xbox.
Andrew Russell