I have several issues with playing music in a Silverlight+XNA combined application. It has several Silverlight pages and the “game” page rendered in XNA.
First, I added the following code to the OnNavigatedTo event of the main Silverlight page:
var song = (Application.Current as App).Content.Load<Song>("Music/Menu");
MediaPlayer.Play(song);
MediaPlayer.Volume = 0.9f;
MediaPlayer.IsRepeating = true;
The music starts playing, however the application fails with the following exception that has no evident callstack hints:
InvalidOperationException occured
FrameworkDispatcher.Update has not been called. Regular FrameworkDispatcher.Update calls are necessary for fire and forget sound effects and framework events to function correctly.
Secondly, I have a few Silverlight-based windows rendered using UIElementRenderer. When the need arises, I change the control’s IsEnabled flag to true and start drawing it on the screen. This makes the music stop playing with a fadeout for no apparent reason and it wouldn’t even start back when I close the Silverligh window.
Navigating from the frame and back to it makes things even worse. The same call to MediaPlayer.Play(song) says that the song has been disposed, even though I load it from the ContentManager anew.
Please see this StackOverflow question — answer is pretty clear. In short, you have to call
FrameworkDispatcher.Update()before using XNA media libraries.