如何使用AudioTrackStreamer播放MP3?

时间:2013-07-28 09:47:22

标签: c# windows-phone-7 streaming

我正在尝试创建一个mp3流媒体,但我失败了: - )

我的AudioTrackStreamer中有以下内容:

        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消息:

[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文件...所以来源应该是合法的?

1 个答案:

答案 0 :(得分:1)

  1. 您正在使用错误的数据源流。在完成处理程序调用{​​{1}}中将[Begin/End]GetRequestStream替换为[Begin/End]GetResponse以访问正确的流。
  2. 请勿从WebResponse.GetResponseStream致电NotifyComplete。相反,请在播放曲目时调用OnBeginStreaming
  3. NotifyComplete之前,您应该设置BeginGetResponse,否则您的MP3将在播放前完全下载。
  4. 更新 - 我的while()循环如下所示:

    req.AllowReadStreamBuffering = false;