通过web服务暂停流媒体广播
本文关键字:流媒体 广播 暂停 服务 web 通过 | 更新日期: 2023-09-27 18:04: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;
}
然后我在webmethod中调用它,像这样
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