c# - 通过网络服务暂停流媒体广播

标签 c# web-services audio-streaming naudio internet-radio

我在开始播放 radio 后遇到问题,暂停功能不起作用这是我的代码

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”,因为它没有在播放功能中修改,我需要找到一种方法来获得它们之间的连接。任何想法!!!!

最佳答案

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

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

关于c# - 通过网络服务暂停流媒体广播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15405556/

相关文章:

c# - 通用实现,但构造函数参数取决于具体类

c# - 与 SQL Azure 的多对多流畅 NHibernate 映射

java - 如何从独立的 Java 客户端调用 Web 服务?

Android 从 VLC/ffmpeg 接收 RTP/UDP 音频流

android - 在 Android 中使用 AudioTrack 播放 WAV 文件

c# - 使用 powershell 脚本通过 c# 重新启动应用程序池

c# - 如何使用 .Net 让窗口保持在顶部?

java - 具有嵌入式 Jetty 服务的 JAX-RS - 主页 URL

c# - 您如何为开发/阶段/生产维护单独的 Web 服务

javascript - 为什么AudioBufferSourceNode会在播放中堆叠?