[U8.1]将在线音频流设置为media元素 [英] [U8.1] Set online audio stream to media element
问题描述
我正在尝试在媒体元素中设置流。这是来自URI的在线音频流(AAC)(例如,无线电流)。问题是我需要从这个流接收元数据,所以我不能直接设置媒体元素的链接。
我必须手动接收流(response.GetResponseStream();)并设置它到media元素,但方法SetSource只接受IRandomAccessStream。
我对MediaStreamSource了解不多,请告诉我,有什么更好的方法来解决我的问题?也许有现成的解决方案或示例?
I'm trying to set stream in media element. This is online audio stream (AAC) from URI (radio stream, for example). The problem is that I need receive the metadata from this stream, so I can not set link to media element directly.
I have to receive the stream manually (response.GetResponseStream();) and set it to media element, but the method SetSource only takes IRandomAccessStream.
I don't know much about MediaStreamSource, so tell me, what better way to solve my problem? Perhaps there are ready-made solutions or examples?
推荐答案
嗨安东,
请阅读粘贴帖子,尤其是
发布指南:主题行标记 和
Windows 10 SDK和工具的已知问题
Please read the sticky posts, especially the Guide to posting: subject line tags and Known Issues for Windows 10 SDK and Tools
是的,你是对的,mediaElement的SetSource需要IRandomAccessStream,所以我们需要使用以下代码将流转换为IRandomAccessStream:
Yes, you are right, the mediaElement's SetSource needs the IRandomAccessStream, so we need to convert the stream to the IRandomAccessStream by using the following code:
private async void Button_Click(object sender, RoutedEventArgs e)
{
Stream stream = response.GetResponseStream();
MemoryStream ms = new MemoryStream();
stream.CopyTo(ms);
IRandomAccessStream Adas = await ConvertToRandomAccessStream(ms);
}
public static async Task<IRandomAccessStream> ConvertToRandomAccessStream(MemoryStream memoryStream)
{
var randomAccessStream = new InMemoryRandomAccessStream();
var outputStream = randomAccessStream.GetOutputStreamAt(0);
var dw = new DataWriter(outputStream);
var task = Task.Factory.StartNew(() => dw.WriteBytes(memoryStream.ToArray()));
await task;
await dw.StoreAsync();
await outputStream.FlushAsync();
return randomAccessStream;
}
最好的问候,
Amy Peng < span style ="color:#0000ff">
Best Regards,
Amy Peng
这篇关于[U8.1]将在线音频流设置为media元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!