The issue you are seeing with your MediaElement
is that you are defining it to be part of the application page and it stops playing as soon as it disappears off of the Visual Tree (i.e. after OnNavigatedFrom
).
If you define a MediaElement
to be "visible" as part of the application frame, audio will keep playing while your app is active (you will need to handle deactivation events, naturally).
If you do this MediaElement
should work for your "background audio".
Be aware you can only have one single active MediaElement
playing media in your app, however you should be able to use SoundEffect
for your sound effects.
Update:
To put your MediaElement
in a frame, you will need to create a custom PhoneApplicationFrame
class/XAML, add the MediaElement
to that XAML, and refer to your custom frame in App.xaml.cs.
// Do not add any additional code to this method
private void InitializePhoneApplication()
{
if (phoneApplicationInitialized)
return;
// Create the frame but don't set it as RootVisual yet; this allows the splash
// screen to remain active until the application is ready to render.
RootFrame = new MyCustomPhoneApplicationFrame();
RootFrame.Navigated += CompleteInitializePhoneApplication;
See this Dzone article for more about Frame/Page in Windows Phone.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…