0

所以我正在做一个简单的钢琴并试图遍历我存储音符的集合,但是 SoundPlayer 不想在“没有调试模式”下正确播放它们,只播放最后一个。但是,当我在那里放一个断点时,它会播放所有这些

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

音高只是链接到文件的序号。
提前致谢

4

2 回答 2

0

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

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

这里是 SoundPlayer 文档的链接

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

于 2015-12-11T08:33:20.797 回答
0

你最好用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();
           }
    }
于 2015-12-11T08:42:06.317 回答