2015-12-11 41 views
0

因此,我正在做一個簡單的鋼琴並嘗試遍歷存儲音符的集合,但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(); 
     } 
    } 

間距僅僅是序號鏈接到文件中。
在此先感謝

回答

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

不客氣的兄弟,你需要給我們至少一個投票或標記答案:) –

0

我覺得它更好,當你使用PlaySync();而不是Play();

因爲那麼你不需要Stop()方法。

Here a link to the docu of SoundPlayer

爲什麼使用PlaySync?如果您只是在此程序中調用Play方法,程序將在聲音播放之前終止。同步指示程序應在播放聲音時暫停。

+0

非常感謝!我期待與線程等東西,但它很容易。 –