Search code examples
c#windows-phone

Why the (httpwebresponse) ResponseStream stops reading data from internet radio?


I am using this code to get the data from an icecast radio, but the ResponseStream stops reading data at 64K recieved. Can you help me with this?

HttpWebRequest request = (HttpWebRequest) WebRequest.Create("http://icecast6.play.cz/radio1-128.mp3");

request.AllowReadStreamBuffering = false;

request.Method = "GET";

request.BeginGetResponse(new AsyncCallback(GetShoutAsync), request);

void GetShoutAsync(IAsyncResult res)

{

    HttpWebRequest request = (HttpWebRequest) res.AsyncState;

    HttpWebResponse response = (HttpWebResponse) request.EndGetResponse(res);

    Stream r = response.GetResponseStream();

    byte[] data = new byte[4096];

    int read;

    while ((read = r.Read(data, 0, data.Length)) > 0)

    {

        Debug.WriteLine(data[0]);

    }

}

Solution

  • I don’t see any obvious problems in your code. Apart from not using async-await which greatly simplifies the kind of asyncronous code you’re developing :-)

    What do you mean “the ResponseStream stops reading”?

    If the connection is dropped, then my #1 idea — server does that. Use wireshark to confirm, and then use wireshark to compare the request’s HTTP header with e.g. Winamp that starts playing that stream. I’m sure you’ll find some important differences.

    If however it merely pauses, it’s normal.

    Upon connect, streaming servers typically send you some initial amount of data, and then they only send you their data in real-time. So, after you’ve received that initial buffer, you’ll only get the data @ the rate of your stream, i.e. 16 kbytes/sec for your 128 kbit/sec radio. BTW, some clients send “Initial-Burst” HTTP header with the request, but I was unable to find the documentation about that header. When I worked on my radio for WP7, I basically replicated the behavior of some other, iOS app.