使用 SoundPlayer 播放 collection 中的音乐

Playing music from collection with SoundPlayer

所以我正在弹一架简单的钢琴并尝试遍历我存储音符的 collection,但 SoundPlayer 不想在 "without debugging mode" 中正确播放它们,只播放最后一个一。然而,当我在那里放置一个断点时,它会播放所有这些

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 只是 link 的序数。
提前致谢

我认为使用 PlaySync() 会更好;而不是 Play();

因为这样你就不需要 Stop() 方法了。

Here a link to the docu of SoundPlayer

为什么要使用 PlaySync?如果您只是在该程序中调用 Play 方法,程序将在声音播放之前终止。 Sync 表示程序应该在播放声音时暂停。

你最好使用 PlaySyn 来告诉你的程序等待音乐播放完毕

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