Click here to Skip to main content
16,017,238 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
This is my code

System.Media.SoundPlayer player = new System.Media.SoundPlayer();
 private void BeatDetector_Move(object sender, EventArgs e)
 {
     if (panel1.Contains(label))
     {
         if (BeatDetector.Location.X == label.Location.X)
         {


             if (label.Text == "KICK2")
             {
            SoundPlayer player = new SoundPlayer(Properties.Resources.KICK2);
            player.Play();
             }



         }
     }
 }


Now in this program I will be adding allot more audio files like 100 or more, and I was wondering if there was a simpler way o write this code? Because I will have to repeat this part of the code over and over again with different type audio file

C#
if (label.Text == "KICK2")
          {
         SoundPlayer player = new SoundPlayer(Properties.Resources.KICK2);
         player.Play();
          }
Posted

You may use a Dictionary[^] for storing the pairs like {"KICK2", Properties.Resources.KICK2} and use a single call. See the code sample at the Dictionary documentation page I linked.
 
Share this answer
 
i think SoundPlayer is too api function
C#
PlaySound("explosion.wav", NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);

but you can use powerfull tools like wmp.dll...
you can add sound to a play list and play aound file...
if you wana simple to use this .. you can use url property..
 
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