Question

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();
   }
}
Was it helpful?

Solution

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top