0

So I am doing a simple piano and trying to traverse the collection where I stored the notes, but the SoundPlayer doesn't want to play them properly in "without debugging mode", playing only the last one. However when I put a breakpoint there it plays all of them

public static List<MusicNote> music = new List<MusicNote>(15);
public static void PlayAll()
    {
        SoundPlayer sp = new SoundPlayer();
        for (int i = 0; i <= music.Count - 1; i++)
        {
            string text = music[i].pitch.ToString();
            sp.SoundLocation = (@"c:\my path here\" + text + ".wav");
            sp.Play();
            sp.Stop();
        }
    }

Pitch is simply ordinal number to link to file.
Thanks in advance

Gleb Eliseev
  • 65
  • 11

2 Answers2

0

I think its better when you use PlaySync(); instead of Play();

Because then you don't need the Stop() methode.

Here a link to the docu of SoundPlayer

Why use PlaySync? If you just call the Play method in this program, the program will terminate before the sound plays. The Sync indicates that the program should pause while the sound plays.

ascholz
  • 179
  • 1
  • 9
0

U better use PlaySyn in order to tell your program to wait until the music complete

    // Create new SoundPlayer in the using statement.
    using (SoundPlayer player = new SoundPlayer())
    {
      for (int i = 0; i <= music.Count - 1; i++)
           {
               string text = music[i].pitch.ToString();
               sp.SoundLocation = (@"c:\my path here\" + text + ".wav");
               // Use PlaySync to load and then play the sound.
               // ... The program will pause until the sound is complete.
               player.PlaySync();
           }
    }
MRebai
  • 5,344
  • 3
  • 33
  • 52