MediaPlayer changes in XNA Game Studio 4.0 for Windows Phone

MediaPlayer, the interface for playing music on Windows Phone, is seeing some small tweaks that can affect applications. These changes are visible in the emulator that comes with the January update to the Windows Phone Developer Tools and will become more visible when the next update for Windows Phones rolls out through Zune.

The short is that you are no longer allowed to set the Volume or IsMuted properties when the active media source is the Zune media library and any time you switch between library music and title music, you must reset any properties on MediaPlayer you care about. These changes were made to ensure applications didn’t alter the user’s music playback experience in a way that the native Zune media player cannot reverse (there is no UI in the Zune media player for music-specific volume or a mute control).

You can see this change today in the emulator. If you were to run the following code you would see that the Volume and IsMuted are ignored while media from the user’s library is playing:

MediaPlayer.Play(new MediaLibrary().Songs);

MediaPlayer.Volume = .5f;

MediaPlayer.IsMuted = true;

Debug.WriteLine("Volume: {0}, IsMuted: {1}", MediaPlayer.Volume, MediaPlayer.IsMuted);

Output:

Volume: 1, IsMuted: False

While the change does affect certain applications attempting to replace the default Zune media player, for some apps and games that play their own music there is a side effect that may cause issues. Because the user may be listening to music on their device when your game starts, if you are setting the Volume or IsMuted properties for your own title music those properties may be ignored.

For example, a common pattern with music playback was something like this where the code would load the song, set the properties accordingly, and then start song playback:

Song song = Content.Load<Song>("BackgroundMusic");

MediaPlayer.Volume = 0.5f;

MediaPlayer.Play(song);

This worked in RTM because we didn’t care what the active media source was; we would set those properties for whatever was playing. With our change this can have issues if the user is listening to music when you try to set the volume.

Moving forward we always recommend playing your music before setting any of the MediaPlayer properties to ensure they are applied to your game’s music:

Song song = Content.Load<Song>("BackgroundMusic");

MediaPlayer.Play(song);

MediaPlayer.Volume = 0.5f;

If you follow this new pattern, it will ensure your title will continue to work as expected when the update rolls out to users. The good part is that you can title update your game today and it will work on the RTM version of Windows Phone as well as the upcoming update.