Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
869 views
in Technique[技术] by (71.8m points)

c# - Play Background Sound and Sound Effect in Windows Phone 8

I have researched a lot on playing sounds for Windows Phone 8 devices and found multiple solutions but they don't quite match my case.

What I need : I'm writing an app (C#+XAML) that uses a file as background sound (must be active while navigating the whole app), and also to be able to play sound effects.

What are the issues :

  • For background sound I could use the BackgroundAudio Agent, but it doesn't meet my requirements because I want the sound to be played only in the background of my app, and to stop if my app closes or is not active.
  • For sound effects - I tried MediaElement which is okay, but I couldn't manage to make it somehow play while I am navigating the whole app. Media closes if I leave that page - I guess I could use this for the sound effects trick. Also, there's the SoundEffect which is not quite a good solution since it can play only .wav files... I could use it for sound effects only but not background sound (big sized files).

So, how should I proceed to play background sound (only inside my app) if I choose MediaElement/SoundEffect to play a sound effect in the app. I need a solution that would allow me to play 2 sounds at once (background and sound effect) and the background sound to be played only while the app runs (is active)...

So far I am confused and managed only to solve the sound effects issue.

Any suggestions are greatly appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...