C# 播放声音列表

C# Play a list of sounds

我有一个简单的钢琴键盘,每次按下一个键都会播放一个音符。我想将每个音符存储在一个列表中,然后在按钮 "Play is pressed" 时依次播放所有音符。我已经创建了列表并将按下的每个音符添加到列表中。但是我在遍历列表和播放每个声音时遇到问题。

    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();
        }
    }

不幸的是,按下按钮后只播放最后一个音符。

原因可能是你没有等到每个声音结束,声音就变成了"overridden"。这样你只能听到最后一个。在迭代中继续播放另一个声音之前,您需要确保播放完声音。

假设您的代码中的 spSoundPlayer class 的实例。您可以使用 sp.PlaySync() 来确保您的程序在移动到下一个声音之前暂停。