2012-03-10 91 views
2

我正在尋找一種播放MP3文件而沒有任何第三方播放它的方式(媒體播放器等)有沒有辦法做到這一點? 謝謝。使用C#播放MP3文件#

+0

的([使用C#中的WinForms播放聲音]可能重複http://stackoverflow.com/questions/1304223/playing- winform-using-c-sharp) – Alejandro 2014-06-14 15:34:10

回答

8

我已經寫了叫NAudio一個開放源碼庫,可以這樣做:

private IWavePlayer waveOut; 
private Mp3FileReader mp3FileReader; 

private void PlayMp3() 
{ 
    this.waveOut = new WaveOut(); // or new WaveOutEvent() if you are not using WinForms/WPF 
    this.mp3FileReader = new Mp3FileReader("myfile.mp3"); 
    this.waveOut.Init(mp3FileReader); 
    this.waveOut.Play(); 
    this.waveOut.PlaybackStopped += OnPlaybackStopped; 
} 

private void OnPlaybackStopped(object sender, EventArgs e) 
{ 
    this.waveOut.Dispose(); 
    this.mp3FileReader.Dispose(); 
} 
+0

謝謝,我想這正是我所期待的,OnPlaybackStopped事件在歌曲到達時被解僱了? – idish 2012-03-10 10:12:59

+0

@idish,NAudio有很好的文檔記錄,對於你的問題,只需使用它,你會看到答案。 – 2012-03-10 11:13:18

+0

好的,謝謝。 – idish 2012-03-10 11:41:04

0

我不明白你爲什麼要避開第三方庫。如果你使用c#編寫代碼,你可能正在開發windows,通常有winmm.dll。所以你可以導入並使用mciSendStringlike in this example

但是,如果您將MP3數據轉換爲原始數據,則可以使用.NET SoundPlayer類進行播放。