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();