Freigeben über


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.

Comments

  • Anonymous
    March 18, 2011
    Is there any possibility that this could cause a "chirp" at the start of a song , when implementing a fade-in effect?

  • Anonymous
    March 18, 2011
    It is quite unlikely and isn't something we've seen during testing. One could play a blank "song" that is just a second or two of silence, set the properties, and then play their real music.

  • Anonymous
    March 22, 2011
    Wow.  So now (whenever I get the update) I won't have to reboot my phone (which is actually quite fast!) or track down whatever new game I recently muted (a common scenario) just to get audio to play again in the Zune app?  Cool. Out of curiosity, does WP7 Silverlight's MediaElement internally set its Volume and IsMuted properties after the specified Source starts playing?  Maybe it doesn't matter, since it's not directly manipulating XNA's static MediaPlayer class.

  • Anonymous
    March 25, 2011
    Glad this change made you happy. I'm sure you're not the only one who's had apps accidentally make your music unlistenable. Once this goes out, you'll never have to worry again. :) I do not know how Silverlight's MediaElement interacts with the native media system, sorry.

  • Anonymous
    March 28, 2011
    The comment has been removed

  • Anonymous
    March 29, 2011
    No, with the update you will no longer be able to change the volume of the MediaPlayer while the content playing is from the Zune library.

  • Anonymous
    March 29, 2011
    not even of a different program, as it was the case before ?

  • Anonymous
    March 30, 2011
    Sorry i'm not sure i get it. I've got my real wp7 device and i am playing a SoundEffectInstance on which i need to set the volume to 1 and then lower whatever my currently playing music is so that i can hear the soundeffect. This is in a silverlight app. I recently upgraded my real phone device but i dont have copy and paste on it - i think that was an update to prep it ready for the copy paste release right - which is the release you are describing here....? So currently my music volume as played by the zune player lowers perfectly and i can hear the soundeffect voice speak. once my voice stops the music volume increases. perfect. It would be a very bad step if you stop this from happening with soundeffectinstance because otherwise the soundeffects are not able to be heard properly - and if they are voice instructions - the user experience becomes bad. the truth is you have gone about this wrong. what you should have done is first consult someone else because to my mind you would allow us to set a relative volume on a soundeffectinstance in relation to the mediaplayer.  so if i said it was 2 - then this mixes my sound at twice the volume of the mediaplayer just for the duration of my playing sound.  obviously it means my sound plays at 1 and mediaplayer plays at .5 until my sound completes. this way i wouldnt change the mediaplayers volume per se, but what would happen with the os is that it would take the currently highest app mis ratio at any given time and apply that to the audio mixing. that's multitasking sorted - which is clearly what this is all about.

  • Anonymous
    March 30, 2011
    ...and what is so annoying is that i was just about to release to market. now i have no way of hearing the voice in my app because the music drowns it out. nice one! this could easily be fixed IF you just allow SoundEffect.MasterVolume to go above 1f.  If you did this i would replace my MediaPlayer.Volume = 0.1f with SoundEfect.MasterVolume = 10f; and my MediaPlayer.Volume = 1f; with SoundEffect.MasterVolume = 1f;

  • Anonymous
    March 30, 2011
    seriously - withold the release and go back to the drawing board. you guys aren;t ready yet and you havent thought this through properly.

  • Anonymous
    April 01, 2011
    Jason - This change comes in the update in March Update with copy/paste. To solve your problem you could author your sound effects at a higher volume if setting SoundEffect.MasterVolume to 1 is not sufficient enough for making your sound effects audible over the music. Additionally you could pause the music altogether, play your sound effect, and then resume the music. Both options will allow you to finish the app you're describing with the changes I mentioned above. Before this change games and apps could mute Zune music playback with no way to fix it short of rebooting the phone. This was a terrible user experience. The change will cause issues with some apps, but we had to do right by the customer by protecting their music experience.

  • Anonymous
    April 01, 2011
    Hey nick Tell you what, I will release as is, and put a pause option in also and send u a link to Zune marketplace. I know which one you'd prefer. Why u couldn't reset Zune on app tombstone or shutdown? That's how i restore volume 1 but u could do that at os level . I know we have multitasking to consider but not being able to put audibles in front of others is like playing anims behind a locked screen. If u were blind you'd change ur mind. There is a very strong use case for blending sound to achieve a maximum experience. Cheers

  • Anonymous
    April 07, 2011
    Hi Nick in OnNavigatedTo of my application I use this code "Song currentSong = MediaPlayer.Queue.ActiveSong;" first of NoDo updeate ActiveSong had ever a valid value but now it is valid only if MediaPlayer is in playing state. If MediaPlayer is paused ActiveSong hasn't a valid value. How I can obtain ActiveSong value when MediaPlayer is paused? Sorry for my bad english. tks.

  • Anonymous
    April 12, 2011
    Hi Nick If you have access to both versions (as you would) see which one handle this use case best. For this scenario you'll need a better way of handling this going forward.  The app is free to try, Outdoor Trekker. social.zune.net/redirect

  • Anonymous
    April 12, 2011
    actually thats the link for outdoor toolkit (but its the same deal - they both have the same use case described)

  • Anonymous
    December 28, 2011
    The comment has been removed