Click here to Skip to main content
15,902,840 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have developed a simple application of FlipFlop game. I have 16 buttons on the form and i want to add music file as background music but as soon as i click any button, music stops. I did this:
MediaPlayer mp = new MediaPlayer();
mp.Open(new Uri(@"101. Shahin & Sepehr - Tears Of Fire.mp3", UriKind.Relative));
mp.Play();
Posted
Updated 4-Jan-15 5:50am
v2
Comments
BillWoodruff 4-Jan-15 3:32am    
What Button is clicked: what does the code in that Button's Click EventHandler do ? Are you playing the music in a loop ? We can't read your mind.
Member 11345989 4-Jan-15 11:55am    
I have 16 buttons on the form and each button has a picture at the back side. Buttons are clicked for image flip. when a button is clicked, image is shown. I tried to add music in the game. Music plays fine when form is loaded for the first time. But when any of the buttons is clicked, music stops and never plays again. I want the music to remain unchanged (keep playing) when any button is clicked.

Look into the BackgroundWorker Class[^].
Play the music in the DoWork event.
 
Share this answer
 
Set below code on your button click event and you have to set only wav file.

C#
protected void btnclick_Click(object sender, EventArgs e)
      {
          string path = Server.MapPath("Songs/welcome.wav");
          playSound(path);
          //System.Threading.Thread.Sleep(2000);
      }

      private void playSound(string path)
      {
          System.Media.SoundPlayer player =
              new System.Media.SoundPlayer();
          player.SoundLocation = path;
          player.Load();
          player.Play();
      }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900