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 (16)

  1. Andrew Russell says:

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

  2. nicgrave says:

    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.

  3. tiwahu says:

    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.

  4. nicgrave says:

    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.

  5. Andy says:

    Hi, I've developed "Media Volume Settings" on the marketplace, exactly to fix that issue. So I guess now the problem is solved by the update (which I have and so it seems), and I'm glad about that.

    On the other hand there was another side effect which was welcomed of my application, which was fine tuning the volume for those applications (or even zune) that had the volume too high, even at 1/30. This happens on different handsets it seems, as they have differently loud speakers (mine is extremely loud).

    Do you think there is some kind of workaround to get that kind of functionality back or any other way to fine tune volume ?

    Best regards

    Andy

  6. nicgrave says:

    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.

  7. not even of a different program, as it was the case before ?

  8. Jason says:

    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.

  9. Jason says:

    …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;

  10. jason says:

    seriously – withold the release and go back to the drawing board. you guys aren;t ready yet and you havent thought this through properly.

  11. nicgrave says:

    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.

  12. jason says:

    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

  13. Pietro says:

    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.

  14. Jason says:

    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

  15. Jason says:

    actually thats the link for outdoor toolkit (but its the same deal – they both have the same use case described)

  16. Chris says:

    I guess this is a pretty old post but what about alarm clock apps that play music from your Zune library?  It's going to make for a pretty crappy experience if the user has to remember to set the volume themselves every night…