因此,我正在做一架简单的钢琴,并试图遍历我存储笔记的集合,但是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();
}
}音高是简单的序号链接到文件。
提前感谢
发布于 2015-12-11 08:42:06
你最好使用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-11 08:33:20
我认为使用PlaySync()而不是Play()更好;
因为这样就不需要停止()方法了。
这里有一个指向SoundPlayer文档的链接
为什么要使用PlaySync?如果您只是在这个程序中调用Play方法,程序将在播放声音之前终止。同步表示在播放声音时,程序应该暂停。
https://stackoverflow.com/questions/34218802
复制相似问题