Silverlight for Windows Phone 7 is based on the page navigation system. Key point is the lifetime of pages – each page is deleted while you navigate to another. To have an object(singleton f.e.) during the whole application life it could be placed in App class (App.xaml.cs file). MediaElement is definitely an object that should be one (there must be only one music player). The best and simple solution is place the MediaElement in XAML(it needs to be a part of visual tree), I have used application resources:
<MediaElement x:Name="mediaPlayer" Source="/Sound/horrorSong.mp3" AutoPlay="False" />
Then you can get it from any page:
MediaElement player = null; // get the media element from App resources
player = App.Current.Resources["mediaPlayer"] as MediaElement;
if (player != null)
MediaElement can’t play the music while Zune is connect, it is good to prompt the user about it.
if (NetworkInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
zuneTextBlock.Visibility = System.Windows.Visibility.Visible;
zuneTextBlock.Visibility = System.Windows.Visibility.Collapsed;
This code does not guarantee if Zune is on (User sill can have the plugged phone) but is very likely that Zune is running(Zune starts while you connect the phone).