【发布时间】:2015-09-24 20:03:24
【问题描述】:
我有一个用于从在线流(视频流或广播)录制一些音乐/视频文件的代码。
如何修改它以将 SetSource 属性设置为我的 MediaElement(例如来自 IBuffer 或 Bytes[],...)?
(我不能像这样使用变体:
mediaElement1.Source = new Uri(urlLinkToOnlineStream); mediaElement1.Play();
)
我需要直接从已经打开的流中设置 SetSource(我需要写入文件并在同一时刻或稍稍停顿一下从流中记录相同的 bytes[])。
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();
【问题讨论】:
-
我有一个连接
Windows.Web.Http.HttpClient和MediaElement的样本:github.com/kiewic/MediaElementWithHttpClient 还有一个System.Net.Http.HttpCient和MediaElement的样本:stackoverflow.com/questions/18594659/…
标签: c# xaml windows-phone-8.1 microsoft-metro windows-10