0

I have a code for recording some music/video file from online stream (video stream or radio).

How can I modify it for setting the SetSource property to my MediaElement (from IBuffer or Bytes[],... for example)?

(I cannot use the variant like this:

mediaElement1.Source = new Uri(urlLinkToOnlineStream); mediaElement1.Play();

)

I need to set SetSource directly from already opened stream (I need write to file and record the same bytes[] from stream in the same moment or with a small pause).

HttpClientHandler aHandler = new HttpClientHandler();
aHandler.ClientCertificateOptions = ClientCertificateOption.Automatic;
HttpClient aClient = new HttpClient(aHandler);
aClient.DefaultRequestHeaders.ExpectContinue = false;
HttpResponseMessage response = await aClient.GetAsync(urlLinkToOnlineStream, HttpCompletionOption.ResponseHeadersRead); 
var destinationFile = await KnownFolders.MusicLibrary.CreateFileAsync(@"recorded.mp3", CreationCollisionOption.ReplaceExisting);
var fileStream = await destinationFile.OpenAsync(FileAccessMode.ReadWrite);

Stream stream = await response.Content.ReadAsStreamAsync();
IInputStream inputStream = stream.AsInputStream();
ulong totalBytesRead = 0;
while (true)
{
    // Read from the web.
    IBuffer buffer = new Windows.Storage.Streams.Buffer(1024);
    buffer = await inputStream.ReadAsync(buffer, buffer.Capacity, InputStreamOptions.None);
    if (buffer.Length == 0)
    {
        break;
    }
    totalBytesRead += buffer.Length;
    await fileStream.WriteAsync(buffer);
}
inputStream.Dispose();
fs.Dispose();
merovinh
  • 1
  • 1
  • 3
  • I have a sample to connect `Windows.Web.Http.HttpClient` with `MediaElement` here: https://github.com/kiewic/MediaElementWithHttpClient And there is a sample of `System.Net.Http.HttpCient` with `MediaElement` here: http://stackoverflow.com/questions/18594659/how-to-play-a-video-stream-that-requires-authentication/18630815#comment53215153_18630815 – kiewic Sep 24 '15 at 23:49

2 Answers2

0

Are you developing the Universal Windows Platform App or Windows Phone 8.1 App? The UWP supports the media stream protocol like HTTP Live Stream. You can assign the URL to the media element directly.

UWP:

media.Source = new Uri("http://amssamples.streaming.mediaservices.windows.net/49b57c87-f5f3-48b3-ba22-c55cfdffa9cb/Sintel.ism/manifest(format=m3u8-aapl)");

For Windows Phone 8.1, you can use the open source stream media library.

For example: Windows Phone Streaming Media

Jeffrey Chen
  • 4,650
  • 1
  • 18
  • 22
0

I know this is old, however I have a working example for a single audio file that can be adapted for a stream.

InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream();
HttpClient hc = new HttpClient();
HttpResponseMessage msg = await hc.GetAsync(urlLinkToOnlineStream);
await RandomAccessStream.CopyAsync(await msg.Content.ReadAsInputStreamAsync(), stream);
stream.Seek(0);
myMediaElement.SetSource(stream, msg.Content.Headers.ContentType.ToString());

You must remember to call Dispose() on your InMemoryRandomAccessStream and HttpClient when you are sure you have finished with them. Otherwise you will most likely end up with a memory leak.

I'm not entirely sure how you intend to do the buffering, but you if you call Dispose() on the stream (in my example) playback will fail. It will also start playing the new chunk of data if you call SetSource before current chunk has finished.

Ne0
  • 2,688
  • 3
  • 35
  • 49