0

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();
        }


    }
Anton Sizikov
  • 9,105
  • 1
  • 28
  • 39
Ashni Goyal
  • 819
  • 3
  • 10
  • 20

1 Answers1

0

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

Community
  • 1
  • 1
Martin Suchan
  • 10,600
  • 3
  • 36
  • 66
  • does this code work for silverlight ? i am getting errors at `song` and `mediaplayer` – Ashni Goyal Oct 10 '12 at 05:33
  • you have to use proper path to your file. I use as path "Resources/Alarms/Alarm05.wma" - the file is then located in folder Resources/Alarms. You might also need the ID_CAP_MEDIALIB capability added to the WMAppManifest.xml file. – Martin Suchan Oct 11 '12 at 09:42