2013-03-14 178 views
2

我有一個probleme一旦我開始玩無線電次暫停功能不起作用此暫停流媒體廣播是我的代碼通過Web服務

public class player 
{ 
    Stream ms = new MemoryStream (); 
    WaveStream blockAlignedStream; 
    IWavePlayer waveOut = new WaveOut (WaveCallbackInfo . FunctionCallback ());` 

    public void play (string url) 
    { 

     new Thread (delegate (object o) 
     { 
      // http://www.samisite.com/sound/cropShadesofGrayMonkees.mp3 
      var response = WebRequest . Create (url) . GetResponse (); 
      using (var stream = response . GetResponseStream ()) 
      { 
       byte[] buffer = new byte [ 65536 ]; // 64KB chunks 
       int read; 
       while ((read = stream . Read (buffer , 0 , buffer . Length)) > 0) 
       { 
        var pos = ms . Position; 
        ms . Position = ms . Length; 
        ms . Write (buffer , 0 , read); 
        ms . Position = pos; 
       } 
      } 
    }) . Start (); 

    // Pre-buffering some data to allow NAudio to start playing 
    while (ms . Length < 65536 * 5) // *10 
     Thread . Sleep (1000); 

    ms . Position = 0; 

    blockAlignedStream = new BlockAlignReductionStream (WaveFormatConversionStream . CreatePcmStream (new Mp3FileReader (ms))); 
    this.waveOut . Init (blockAlignedStream); 
    this.waveOut . Play (); 
    while (waveOut . PlaybackState == PlaybackState . Playing) 
     { 
     System . Threading . Thread . Sleep (100); 
     } 
    } 
public void stop () 
    { 
    this.waveOut . Stop (); 
    this.waveOut . Dispose (); 
    this.waveOut = null; 
    } 

然後我把它像這樣

一個webmethode屏幕消息顯示
player mp3=new player (); 
[WebMethod] 
// http://streaming.radio.funradio.fr/fun-1-44-128 

public void play(string url) 
{ 
    mp3.play (url); 
} 
[WebMethod] 
public void pause () 
{ 
    mp3 .pause(); 
} 

有時這個錯誤:「waveout的設備在WaveOut.Finalize沒有關閉()」

我已經找到了probleme當我啓動了暫停功能,它使用的parametre「WAV eOut「,因爲它沒有在播放函數中修改,我需要找到一種方法來獲得它們之間的連接。有任何想法嗎 !!!!

回答

0

這不是我推薦用於播放流式MP3文件的方法。我很驚訝它可以工作,因爲NAudio中的MP3文件閱讀器試圖建立一個目錄,並且你正試圖在兩個不同的線程上讀寫一個內存流。

NAudio演示應用程序顯示我將如何推薦這樣做。您可以在收到MP3幀後對其進行解析和解碼,並將它們放入BufferedWaveProvider中,該緩衝提供器用於播放。或者,如果您沒有足夠的緩衝音頻,可以暫停播放。源代碼可從naudio.codeplex.com

+0

我必須使用這種方法,因爲我正在構建一個無線服務器。我已經使用了codeplex的例子,並試圖根據我的需要進行調整 – 7addan 2013-03-14 16:49:29