How to create a background music?

眉间皱痕 提交于 2019-12-11 14:09:19

问题


I need to run a background music in WP7 Silverlight application. I need it to keep playing when navigating between pages.

Also I don't want it to stop when another sound effect is played.


回答1:


You can use a MediaElement to do that or use XNA SoundEffects but I have a better solution. Create a SilverXNA project for your app. You don't really need to use XNA but this project automatically creates some stuff which makes life easier. You can use this to build a Silverlight App with no problems.

After creating the project include your sound file in the project say "7am.mp3" and set it's build action to "Content".

Create a song in GamePage.xaml.cs:

Song music;

Now add this in OnNavigatedTo():

music = contentManager.Load<Song>("7am");

After this you can play this song as:

MediaPlayer.Play(music);

You may set the volume etc.

So, why this approach? Well, because according to technical certification guidelines you cannot interrupt a user if he is already listening to a song and SoundEffects class cannot be used for background music thus this method is the only way to get such flexibility.

You can use MediaPlayer.GameHasControl to see if user is playing music or not.




回答2:


The solution was in referencing Microsoft.Xna.Framework then using the following function:

public static void PlaySound(string soundFile)
{
    var stream = Application.GetResourceStream(new Uri(soundFile, UriKind.Relative)).Stream;

    if (stream != null)
    {
        var effect = Microsoft.Xna.Framework.Audio.SoundEffect.FromStream(stream);
        Microsoft.Xna.Framework.FrameworkDispatcher.Update();
        effect.Play();
    }
}

http://spacemigas.wordpress.com/2011/04/07/overcoming-windows-phone-7-mediaelement-limitations/



来源:https://stackoverflow.com/questions/11383946/how-to-create-a-background-music

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!