The read-only property
MediaError.message
返回
DOMString
which contains a human-readable string offering specific diagnostic details related to the error described by the
MediaError
object, or an empty string (
""
) if no diagnostic information can be determined or provided.
var errorMessage = mediaError.message;
A
DOMString
providing a detailed, specific explanation of what went wrong and possibly how it might be fixed. This is
not
simply a generic description of the
MediaError.code
property's value, but instead goes deeper into the specifics of this particular error and its circumstances. If no specific details are available, this string is empty.
此范例创建
<audio>
element, establishes an error handler for it, then lets the user click buttons to choose whether to assign a valid audio file or a missing file to the element's
src
attribute. The error handler simply outputs a message to a box onscreen describing the error, including both the
code
和
message
.
Only the relevant parts of the code are displayed; you can see the complete source code here .
The example creates an
<audio>
element and lets the user assign either a valid music file to it, or a link to a file which doesn't exist. This lets us see the behavior of the
error
event handler, which is received by an event handler we add to the
<audio>
element itself.
The error handler looks like this:
audioElement.onerror = function() {
let s = "";
let err = audioElement.error;
switch(err.code) {
case MediaError.MEDIA_ERR_ABORTED:
s += "The user canceled the audio.";
break;
case MediaError.MEDIA_ERR_NETWORK:
s+= "A network error occurred while fetching the audio.";
break;
case MediaError.MEDIA_ERR_DECODE:
s+= "An error occurred while decoding the audio.";
break;
case MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED:
s+= "The audio is missing or is in a format not supported by your browser.";
break;
默认:
s += "An unknown error occurred.";
break;
}
let message = err.message;
if (message && message.length) {
s += " " + message;
}
displayErrorMessage("<strong>Error " + err.code + ":</strong> " + s + "<br>");
};
This gets the
MediaError
object describing the error from the
error
property on the
HTMLAudioElement
representing the audio player. The error's
code
attribute is checked to determine a generic error message to display, and, if
message
is not empty, it's appended to provide additional details. Then the resulting text is output to the log.
You can try out this example below, and can see the example in action outside this page here .
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
The definition of 'MediaError.message' in that specification. |
实时标准 | 初始定义 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
message
|
Chrome 59 | Edge ≤79 | Firefox 52 | IE ? | Opera 46 | Safari ? | WebView Android 59 | Chrome Android 59 | Firefox Android 52 | Opera Android 43 | Safari iOS ? | Samsung Internet Android 7.0 |
完整支持
兼容性未知
<video>
and
<audio>
MediaError
.
MediaError
code
message
BeforeUnloadEvent
DOMStringMap
ErrorEvent
GlobalEventHandlers
HTMLAnchorElement
HTMLAreaElement
HTMLAudioElement
HTMLBRElement
HTMLBaseElement
HTMLBaseFontElement
HTMLBodyElement
HTMLButtonElement
HTMLCanvasElement
HTMLContentElement
HTMLDListElement
HTMLDataElement
HTMLDataListElement
HTMLDialogElement
HTMLDivElement
HTMLDocument
HTMLElement
HTMLEmbedElement
HTMLFieldSetElement
HTMLFormControlsCollection
HTMLFormElement
HTMLFrameSetElement
HTMLHRElement
HTMLHeadElement
HTMLHeadingElement
HTMLHtmlElement
HTMLIFrameElement
HTMLImageElement
HTMLInputElement
HTMLIsIndexElement
HTMLKeygenElement
HTMLLIElement
HTMLLabelElement
HTMLLegendElement
HTMLLinkElement
HTMLMapElement
HTMLMediaElement
HTMLMetaElement
HTMLMeterElement
HTMLModElement
HTMLOListElement
HTMLObjectElement
HTMLOptGroupElement
HTMLOptionElement
HTMLOptionsCollection
HTMLOutputElement
HTMLParagraphElement
HTMLParamElement
HTMLPictureElement
HTMLPreElement
HTMLProgressElement
HTMLQuoteElement
HTMLScriptElement
HTMLSelectElement
HTMLShadowElement
HTMLSourceElement
HTMLSpanElement
HTMLStyleElement
HTMLTableCaptionElement
HTMLTableCellElement
HTMLTableColElement
HTMLTableDataCellElement
HTMLTableElement
HTMLTableHeaderCellElement
HTMLTableRowElement
HTMLTableSectionElement
HTMLTemplateElement
HTMLTextAreaElement
HTMLTimeElement
HTMLTitleElement
HTMLTrackElement
HTMLUListElement
HTMLUnknownElement
HTMLVideoElement
HashChangeEvent
历史
ImageData
定位
MessageChannel
MessageEvent
MessagePort
Navigator
NavigatorGeolocation
NavigatorID
NavigatorLanguage
NavigatorOnLine
NavigatorPlugins
PageTransitionEvent
Plugin
PluginArray
PopStateEvent
PortCollection
PromiseRejectionEvent
RadioNodeList
Transferable
ValidityState
Window
WindowBase64
WindowEventHandlers
WindowTimers