我正在尝试创建一个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文件...所以来源应该是合法的?
答案 0 :(得分:1)
[Begin/End]GetRequestStream
替换为[Begin/End]GetResponse
以访问正确的流。WebResponse.GetResponseStream
致电NotifyComplete
。相反,请在播放曲目时调用OnBeginStreaming
。NotifyComplete
之前,您应该设置BeginGetResponse
,否则您的MP3将在播放前完全下载。更新 - 我的while()循环如下所示:
req.AllowReadStreamBuffering = false;