【问题标题】:Pause a streaming radio through web services通过网络服务暂停流媒体广播
【发布时间】:2013-03-02 13:59:03
【问题描述】:

我开始播放收音机时遇到问题,暂停功能不起作用这是我的代码

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;  
   }

然后我称它为这样的网络方法

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() 关闭”

我在启动暂停功能时发现了问题,它使用参数“waveOut”,因为它没有在播放功能中修改,我需要找到一种方法来获得它们之间的连接。任何想法!!!!

【问题讨论】:

    标签: c# web-services streaming radio naudio


    【解决方案1】:

    这不是我推荐的播放流 MP3 文件的方法。我很惊讶它的工作原理,因为 NAudio 中的 MP3 文件阅读器尝试构建目录,而您尝试在两个不同的线程上读取和写入内存流。

    NAudio 演示应用程序展示了我建议如何执行此操作。您在收到 MP3 帧时对其进行解析和解码,并将它们放入用于播放的 BufferedWaveProvider 中。或者,如果您没有足够的缓冲音频,您可以暂停播放。源代码来自naudio.codeplex.com

    【讨论】:

    • 我必须使用这种方法,因为我正在构建无线电服务器。而且我已经在使用 codeplex 中的示例并尝试根据我的需要对其进行调整
    猜你喜欢
    • 2012-11-02
    • 2011-03-26
    • 1970-01-01
    • 1970-01-01
    • 2015-09-10
    • 2012-07-21
    • 2011-05-13
    • 1970-01-01
    • 2011-11-08
    相关资源
    最近更新 更多