MediaElement.MediaFailed Event

Definition

Occurs when there is an error associated with the media Source.

public:
 virtual event ExceptionRoutedEventHandler ^ MediaFailed;
// Register
event_token MediaFailed(ExceptionRoutedEventHandler const& handler) const;

// Revoke with event_token
void MediaFailed(event_token const* cookie) const;

// Revoke with event_revoker
MediaElement::MediaFailed_revoker MediaFailed(auto_revoke_t, ExceptionRoutedEventHandler const& handler) const;
public event ExceptionRoutedEventHandler MediaFailed;
function onMediaFailed(eventArgs) { /* Your code */ }
mediaElement.addEventListener("mediafailed", onMediaFailed);
mediaElement.removeEventListener("mediafailed", onMediaFailed);
- or -
mediaElement.onmediafailed = onMediaFailed;
Public Custom Event MediaFailed As ExceptionRoutedEventHandler 
<MediaElement MediaFailed="eventhandler" .../>

Event Type

Examples

The following code creates a MediaFailed event handler that calls a helper function to retrieve the HRESULT from the event arguments.

private void videoMediaElement_MediaFailed(object sender, ExceptionRoutedEventArgs e)
{
    // get HRESULT from event args 
    string hr = GetHresultFromErrorMessage(e);

    // Handle media failed event appropriately 
}

private string GetHresultFromErrorMessage(ExceptionRoutedEventArgs e)
{
    String hr = String.Empty;
    String token = "HRESULT - ";
    const int hrLength = 10;     // eg "0xFFFFFFFF"

    int tokenPos = e.ErrorMessage.IndexOf(token, StringComparison.Ordinal);
    if (tokenPos != -1)
    {
        hr = e.ErrorMessage.Substring(tokenPos + token.Length, hrLength);
    }

    return hr;
}

Remarks

It is a best practice to always handle the MediaFailed event and take appropriate action.

Applies to