MediaElement.MediaFailed Event
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
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;
C#
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
XAML
<MediaElement MediaFailed="eventhandler" .../>
The following code creates a MediaFailed event handler that calls a helper function to retrieve the HRESULT from the event arguments.
C#
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;
}
It is a best practice to always handle the MediaFailed event and take appropriate action.
Product | Versions |
---|---|
WinRT | Build 10240, Build 10586, Build 14383, Build 15063, Build 16299, Build 17134, Build 17763, Build 18362, Build 19041, Build 20348, Build 22000, Build 22621, Build 26100 |