MediaElement.MediaFailed 事件

定义

当存在与媒体 关联的错误时发生。

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

事件类型

示例

以下代码创建一个 MediaFailed 事件处理程序,该事件处理程序调用帮助程序函数以从事件参数检索 HRESULT

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

注解

最佳做法是始终处理 MediaFailed 事件并采取适当的操作。

适用于