Bryan Soltis

Senior Software Engineer
MCPD: Enterprise Application Developer 3.5
MCSD.NET, MCAD, MCP

Silverlight 2.0 Media Element Failed Source

Monday, April 20, 2009 11:51 | posted by: bryan

When developing applications in Silverlight 2.0, there are many new objects and controls.  One of the most popular and useful ones is the media element.  The media element control is used to display audio/video content within the Silverlight application.  In order to display/play a media element, the “source” must first be set.   

medelem1.Source = new Uri(“/audioFiles/audiofile1.mp3”, UriKind.Relative); 

As you will notice, you cannot simply put in a path. You must use the URI class to create your source object.   This process works great, IF you actually have a file for it access.  If your path is incorrect, or the file simply doesn’t exists, the above code will still allow you to set the source.  Then when you try to access the media element, you will be greeted with a lovely Exception error from Silverlight. 

To get around this, I created an event handler for my media element’s  MediaFailed event. 

//Set the failed event handler for the audio element
medelem1.MediaFailed += new EventHandler<ExceptionRoutedEventArgs>( medelem1_MediaFailed); 

Then I add the event handler: 

/// <summary>
/// This function is called when the media element fails to load the audio file.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void medSlide_MediaFailed(object sender, ExceptionRoutedEventArgs e)
{
medelem1.Source = null;
//Uncomment the following line to handle the actual exception
//throw new NotImplementedException();
} 

Hope this saves some of your searching.

Tags:

PermaLink   E-Mail Article   RSS Comment Feed  

Comments

October 17. 2009 00:21

media planning

I found that for doing quick short sounds there was a limit in Silverlight and WPF to the performance and behavior you can expect.

media planning

December 20. 2009 03:38

jeu du keno

A use that I can think of for a socket listener (and I would use SilverLight if it could support it) is that of implementing a pattern where a stream needs to be pre-processed before passing it onto a MediaElement. A good candidate for this is a media stream that contains both meta-data and mp3. The meta-data needs to be filtered out before passing on the mp3 content to the MediaElement.
http://www.keno-sur-internet.fr/

jeu du keno Australia

Add comment


(Will show your Gravatar icon)
Click to change captcha
biuquote
  • Comment
  • Preview
Loading