2

I'm trying to read an audio stream using an HttpClient because I need to modify HTTP headers. The only "working" way I found is the following :

HttpResponseMessage response = await httpClient.GetAsync("http://...");          
Stream stream = await response.Content.ReadAsStreamAsync();
IRandomAccessStream content = stream.AsRandomAccessStream();
musicPlayer.SetSource(content, "audio/mpeg");   //musicPlayer is a MediaElement object
musicPlayer.Play();

The issue is that the MediaElement really starts to play when the file has been entirely downloaded, so it's quite useless for streaming. I need it to play as soon as the stream is received.

theB
  • 6,450
  • 1
  • 28
  • 38
Montikore
  • 21
  • 3
  • You are telling it to download the whole file before you start streaming in your code. Stream stream = await response.Content.ReadAsStreamAsync(); – Ken Tucker Sep 19 '15 at 18:28
  • You could try this link: http://stackoverflow.com/questions/18594659/how-to-play-a-video-stream-that-requires-authentication – kiewic Sep 20 '15 at 05:30
  • I created a converter from `HttpClient` to `IRandomAccessStream`, please give it a try: https://github.com/kiewic/MediaElementWithHttpClient – kiewic Sep 20 '15 at 23:01
  • ok i'll try it as soon as possible. sorry for late response, quit busy... – Montikore Sep 23 '15 at 16:01

1 Answers1

0

You want to use the overload of GetAsync that takes an HttpCompletionOption. The default value is ResponseContentRead, which means that stream will not be available until the content is completely read. Use ResponseHeadersRead instead, which means stream will be available immediately after just the headers have been read.

HttpResponseMessage response = await httpClient.GetAsync("http://...",
    HttpCompletionOption.ResponseHeadersRead);
Todd Menier
  • 37,557
  • 17
  • 150
  • 173
  • it seems to be the solution indeed! thanks! but now i have an error "Cannot use the specified Stream as a Windows Runtime IRandomAccessStream because this Stream does not support seeking" which is true i guess, the stream is not seekable as this is just an mp3 file exposed, but when i do `musicPlayer.Source = new Uri("http://...");` this is working fine. I tried other ways like `IRandomAccessStream content = content.AsInputStream();` but the cast is not possible... do you have any idea how to manage this? – Montikore Sep 20 '15 at 18:00