简体   繁体   中英

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. 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. 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.

For C# code behind wire the event, and add the new event:

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.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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