简体   繁体   中英

Array of sounds in c#

I am trying to make a program that will show images like a slide show, and each time a picture is shown a sound will play for the current image, a different one for each image. How I can use an array with sounds and play the sound from the array?

string[] sounds = new string[] { "nero", "fai", "mpanio", "tv1", "volta", 
                                 "sleep1" }; 
private int currentAudioIndex = 0;

private void timer1_Tick(object sender, EventArgs e)
{
   timer1.Stop();
   try
   {
      timer1.Interval = 5000; // Next time, wait 5 secs
      button1.BackgroundImage = (Image)Properties.Resources.ResourceManager.GetObject(arr1[currentImageIndex]);
      new SoundPlayer(Properties.Resources.nero).Play();// plays only one sound "nero"

      currentImageIndex++;
   }
   finally
   {
      if (currentImageIndex < arr1.Length)
      timer1.Start();
   }
}

I'm assuming you have wav file resources named "nero.wav", "fai.wav", etc...

From there, you can load the resources as a Stream and then pass the stream along to the SoundPlayer constructor:

Stream stream = Properties.Resources.ResourceManager.GetStream(arr1[currentImageIndex] + ".wav");
new SoundPlayer(stream).Play();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM