简体   繁体   English

您如何测试Silverlight是否可以播放流?

[英]How do you test whether Silverlight can play a stream?

I want to write a method like the one bellow. 我想写一个像下面这样的方法。 However, in the event that Silverlight is unable to play the stream natively, I would like it to go through a list of MediaStreamSource classes, and try each one in turn till either one of them works, or it has no more to try. 但是,如果Silverlight无法本地播放该流,我希望它遍历MediaStreamSource类的列表,然后依次尝试每个类,直到它们中的任何一个起作用或没有更多尝试为止。 My question is, how do I tell whether the method below is sufficient for a particular stream. 我的问题是,如何确定以下方法对于特定流是否足够。

public static  void OpenMedia(this MediaElement ME, Stream FileData)
{
  ME.SetSource(FileData);
}

I need some code to execute in the event that this fails to play the media. 如果无法播放媒体,我需要执行一些代码。

I'm not sure if you are looking for testing failure or testing for the ability to play the media type so... 我不确定您是要寻找测试失败还是要测试播放媒体类型的能力...

You can add an event handler for MediaFailed, either in the XAML or he code behind for both cases. 您可以在XAML中添加MediaFailed的事件处理程序,也可以在两种情况下在其后面编写代码。 Checking for playability proactively would take some creativity, such as having some "sample" media files of various types that are very small (< 1 second) and silent which serve the purpose of testing playability and it would be transparent to the user. 主动检查可玩性将需要一些创造力,例如拥有一些很小(<1秒)且无声的各种“样本”媒体文件,这些文件用于测试可玩性,并且对用户是透明的。

For C# code behind wire the event, and add the new event: 对于在事件后面添加代码的C#,并添加新事件:

void yourPage_Loaded(object sender, RoutedEventArgs e)
{
  ME.MediaFailed += new EventHandler<ExceptionRoutedEventArgs>(ME_MediaFailed);
}

void ME_MediaFailed(object sender, ExceptionRoutedEventArgs e)
{
  add your code to handle the exception here.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM