首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用AudioTrackStreamer流式传输MP3?

如何使用AudioTrackStreamer流式传输MP3?
EN

Stack Overflow用户
提问于 2013-07-28 17:47:22
回答 1查看 268关注 0票数 0

我正在尝试创建一个mp3 streamer,但我失败得很糟糕:-)

我的AudioTrackStreamer中有以下内容

代码语言:javascript
复制
        protected override void OnBeginStreaming(AudioTrack track, AudioStreamer streamer)
        {
            //TODO: Set the SetSource property of streamer to a MSS source
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://api.soundcloud.com/tracks/85085126/stream?consumer_key=db840ada2477a93d5fdbcc96a46b37c1");
            req.ContentType = "application/octet-stream";

            try
            {
                req.BeginGetRequestStream((callback) =>
                    {
                        HttpWebRequest request = (HttpWebRequest)callback.AsyncState;
                        Stream stream = request.EndGetRequestStream(callback);
                        Mp3MediaStreamSource src = new Mp3MediaStreamSource(stream, 1000);

                        streamer.SetSource(src);

                        NotifyComplete();

                    }, req);
            }
            catch { }

它失败了,捕捉到了ProtocolIViolationException的消息:

代码语言:javascript
复制
[Arg_InvalidOperationException]
Arguments: 
Debugging resource strings are unavailable. Often the key and arguments provide sufficient information to diagnose the problem. See http://go.microsoft.com/fwlink/?linkid=106663&Version=4.0.50829.0&File=mscorlib.dll&Key=Arg_InvalidOperationException

这个文件的网址是我在谷歌搜索免费音乐时发现的,如果你打开它,你会下载一个mp3文件……所以消息来源应该是合法的?

EN

回答 1

Stack Overflow用户

发布于 2013-07-28 23:27:07

  1. 您对数据源使用了错误的流。而是在曲目播放到最后时调用NotifyComplete
  2. BeginGetResponse之前,您应该设置req.AllowReadStreamBuffering = false;,否则您的MP3s将在播放前完全下载。

Update - my while()循环看起来像这样:

代码语言:javascript
复制
while( true )
{
    if( cancel.IsCancellationRequested )
        return;

    if( buffer.hasFreeSpace() )
    {
        int cb = await this.downloadAsync().ConfigureAwait( false );
        var buffWaiter = m_bufferAwaiter;
        if( 0 == cb )
        {
            Exception ex = new Exception( "runBackgroundAsync: connection dropped" );;
            if( null != buffWaiter )
                buffWaiter.Fail( ex );
            throw ex;
        }

        if( null != buffWaiter )
        {
            // The client is waiting for the data to be pre-buffered
            if( buffWaiter.haveBuffer( buffer.cbLength ) )
            {
                // Sufficient data has been downloaded, so playback is going to resume after this haveBuffer() call.
                m_bufferAwaiter = null;
            }
        }
        continue;   //< Continuing to while(true) without a single sleep.
    }
    await TaskEx.Delay( this.tsSleepTimeWhenBufferFull, cancel ).ConfigureAwait( false );
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17906957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档