Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用SoundPlayer播放收藏中的音乐_C#_Soundplayer - Fatal编程技术网

C# 使用SoundPlayer播放收藏中的音乐

C# 使用SoundPlayer播放收藏中的音乐,c#,soundplayer,C#,Soundplayer,因此,我正在做一个简单的钢琴,并试图遍历我存储音符的集合,但SoundPlayer不想在“未调试模式”下正确播放它们,只播放最后一个音符。然而,当我把一个断点放在那里时,它会播放所有的断点 public static List<MusicNote> music = new List<MusicNote>(15); public static void PlayAll() { SoundPlayer sp = new SoundPlayer();

因此,我正在做一个简单的钢琴,并试图遍历我存储音符的集合,但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();
        }
    }
公共静态列表音乐=新列表(15);
公共静态void PlayAll()
{
SoundPlayer sp=新的SoundPlayer();

对于(int i=0;i我认为使用PlaySync()而不是Play()更好

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


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

U最好使用
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();
           }
    }
//在using语句中创建新的SoundPlayer。
使用(SoundPlayer=新的SoundPlayer())
{

对于(int i=0;我非常感谢!我本来希望有一些线程之类的东西,但结果很简单。不客气,兄弟,你至少需要给我们投赞成票或标出答案:)