C# Silverlight MediaElement 无法从网络播放 mp3
您好,过去几天我一直在为此烦恼,我在 Silverlight 应用程序中有一个用 C# 编写的媒体元素,
我试图通过 http 流式传输 MP3 并通过 silverlight 应用程序播放。尽管它不断捕获一个异常:
AG_E_NETWORK_ERROR
mp3 不会播放,并且 MediaElement.MediaOpened 事件永远不会触发
在网上搜索后,此错误似乎是由于 curropt 源造成的,但当我将网络浏览器指向 mp3 时,mp3 播放正常。
这是代码
private void button1_Click(object sender, RoutedEventArgs e)
{
Uri source = new Uri("http://www.sm-testing.co.uk/mixes/youdontknow.mp3");
mediaElement1.Source = source;
mediaElement1.MediaFailed += new EventHandler<ExceptionRoutedEventArgs>(mediaElement1_MediaFailed);
mediaElement1.MediaOpened +=new RoutedEventHandler(mediaElement1_MediaOpened);
}
void mediaElement1_MediaFailed(object sender, ExceptionRoutedEventArgs e)
{
MessageBox.Show( e.ErrorException.ToString());
}
private void mediaElement1_MediaOpened(object sender, RoutedEventArgs e)
{
mediaElement1.Play();
}
Hi Ive been pulling my hair out over this for the last couple of days, I have a mediaelement in a Silverlight App written in C#
Im trying to stream an MP3 over http and play it through the silverlight app. Although it keeps catching an exception that says
AG_E_NETWORK_ERROR
The mp3 will not play and the MediaElement.MediaOpened event never fires
After searching on the net it appears this error is due to a curropt source but the mp3 plays fine when I point a webbrowser at the mp3.
heres the code
private void button1_Click(object sender, RoutedEventArgs e)
{
Uri source = new Uri("http://www.sm-testing.co.uk/mixes/youdontknow.mp3");
mediaElement1.Source = source;
mediaElement1.MediaFailed += new EventHandler<ExceptionRoutedEventArgs>(mediaElement1_MediaFailed);
mediaElement1.MediaOpened +=new RoutedEventHandler(mediaElement1_MediaOpened);
}
void mediaElement1_MediaFailed(object sender, ExceptionRoutedEventArgs e)
{
MessageBox.Show( e.ErrorException.ToString());
}
private void mediaElement1_MediaOpened(object sender, RoutedEventArgs e)
{
mediaElement1.Play();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现您在示例中使用了实际的 URL。我检查了您的网站,没有 http://www.sm-testing.co.uk /ClientAccessPolicy.xml 文件或 http://www.sm-testing.co.uk/crossdomain.xml 文件。
没有这些文件中的任何一个(最好是 ClientAccessPolicy.xml,因为另一个是旧的 Flash 兼容格式并且缺少一些功能)Silverlight 将仅从其托管的域中检索文件。这是一项安全功能,可阻止 Silverlight 应用程序在未经许可的情况下从任何地方获取数据或图像。
下面的 ClientAccessPolicy.xml 示例允许对任何 http 或 https 请求进行任何访问:
您也不需要 crossdomain.xml,除非您也想开放对 Flash 应用程序的访问:)
I see you have used an actual URL in your example. I checked your site and there is no http://www.sm-testing.co.uk/ClientAccessPolicy.xml file or http://www.sm-testing.co.uk/crossdomain.xml file.
Without either of those files (preferably ClientAccessPolicy.xml as the other is an old Flash compatibility format and missing some features) Silverlight will only retrieve files from the domain it is hosted on. This is a security feature to stop Silverlight app taking data or images from just anywhere without permission.
Example ClientAccessPolicy.xml below allows any access to any http or https request:
You don't need a crossdomain.xml as well unless you want to open up access to Flash apps too :)