简体   繁体   中英

C# Play a list of sounds

I have a simple piano keyboard that plays a note each time a key is pressed. I want to store each note in a List and then play all the notes after each other when a button "Play is pressed". I already created the List and am adding each note pressed to the List. But I am having problems traversing the list and playing each sound.

    MusicNote mn = new MusicNote(count, duration, bNoteShape, xLoc, mk.musicNote);
    notes.Add(mn);
    mn.Location = new Point(xLoc, yLoc);
    this.panel1.Controls.Add(mn.drawNote());


    private void Play_Click(object sender, EventArgs e)
    {
        foreach(MusicNote mn in notes)
        {
            textBox2.Text += mn.ToString();
            sp.SoundLocation = @"C:/Users/Daryl/Desktop/mapped/" + mn.musicNote + ".wav"; //change this to the location of your "mapped" folder
            sp.Play();
        }
    }

Unfortunately, only the last note is being played once the button is pressed.

The reason is probably that you don't wait each sound to finish and the sound gets "overridden". This way you hear only the last one. You need to make sure playing a sound is finished before you continue to the other one on your iteration.

Assuming sp in your code is an instance of SoundPlayer class. You can use sp.PlaySync() to make sure your program pauses before moving to the next sound.

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