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:
/// This function is called when the media element fails to load the audio file.
/// <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.