# Playing a Song causing WP7 to crash on phone, but not on emulator

I am trying to implement a song into a game that begins playing and continually loops on Windows Phone 7 via XNA 4.0. On the emulator, this works fine, however when deployed to a phone, it simply gives a black screen before going back to the home screen. Here is the rogue code in question, and commenting this code out makes the app run fine on the phone:

// in the constructor fields
private Song song;

// in the LoadContent() method

// in the Update() method
if (MediaPlayer.GameHasControl && MediaPlayer.State != MediaState.Playing)
{
MediaPlayer.Play(song);
}


The song file itself is a 2:53 long, 2.28mb .wma file at 106kbps bitrate.

Again this works perfectly on emulator but does not run at all on phone. Thanks for any help you can provide!

• Try sticking in a try-catch and check out what exception you're getting. That should help with getting some help on this. – Ray Dey Jan 28 '11 at 23:23
• Good advice. The exception thrown is "System.InvalidOperationException: Song playback failed, please verify that song is not DRM protected." Also, I have tried with the song encoded as an MP3 and got the same results (worked in emulator, does not work in phone). A side question: is there an easy way to print/save debug text on the phone..? My only option was to draw the text on screen, heh. – Michael Zehnich Jan 29 '11 at 4:27

We figured this out. The information is available online but it's really well hidden, so I'll post it here for good measure.

Basically if the phone is tethered to a computer and the Zune software is running, it locks the media on the phone, which means that MediaPlayers in apps will never have permission to play a Song.

In order to get it to work, the user must un-tether the phone (i.e. unplug the USB cord), or use the Windows Phone Connect tool which has been designed with this in mind with regards to debugging: http://msdn.microsoft.com/en-us/library/gg180729%28v=VS.92%29.aspx - I haven't tried the tool, but untethering worked like a charm.

Also, when the app doesn't have permission to play a song, it will crash unless the exception is handled, so make sure to use try/catch blocks. The caught exception can simply be ignored.

• Ah, the joys of DRM. – Jari Komppa Jan 29 '11 at 11:00
• Good to know, annoying as hell as I imagine quite a few developers run into this issue. – Ray Dey Jan 29 '11 at 19:15

The answer from Michael Zehnich is indeed correct. However I stumbled upon another problem that cause the same exception.

The problem for me was that I was missing the ID_CAP_MEDIALIB capability in my WMAppManifest.xml.

<Capability Name="ID_CAP_MEDIALIB"/>


Hope this helps.