Monday, October 8, 2012

Adding two music sources in WP7 app

Adding two music sources in WP7 app

In my WP7 gaming app , I want two music files to run. One is the background music and another follows the user action , say, user kills the enemy. I am using MediaElement to do this. I am facing two issues.

1) How to loop background music ?

2) As soon as second music starts, the first music stops and does not start back when the second music stops. I do not want background music to stop, they should overlap. How to do this ?

I am using silverlight.

XAML

<MediaElement x:Name="stroke"   AutoPlay="False" /> <MediaElement x:Name="bmusic"   AutoPlay="True" /> 

C#

 protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)     {         base.OnNavigatedTo(e);          var settings = IsolatedStorageSettings.ApplicationSettings;         if (settings.Contains("bm"))         {             string hy=(string)settings["bm"];             //check if user has disabled music play             if (hy == "1")             {                 bmplay = 1;                 // play background music                 bmusic.Source = new Uri("bmusic.mp3", UriKind.Relative);                  bmusic.Play();              }             else             {                 bmplay = 0;             }         }         else         {             bmplay = 1;             // play b music             bmusic.Source = new Uri("bmusic.mp3", UriKind.Relative);              bmusic.Play();         }            if (NavigationContext.QueryString.TryGetValue("msg", out msg))         {            //  textBox1.Text +=" "+ msg;              find_move();         }       } 

Answers & Comments...

Answer: 1

For repeating music you can use this:

Song s = Song.FromUri("song", new Uri(path)); FrameworkDispatcher.Update(); MediaPlayer.IsRepeating = repeat; MediaPlayer.Play(s); 

And try to use SoundEffect for sounds, they can be played simultaneously with background music.
Sound effect in windows phone 7

by : Martin Suchanhttp://stackoverflow.com/users/574062




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog