无法设置未定义错误的属性“ onaddtrack”

时间:2020-07-11 08:27:40

标签: javascript html events

我正在学习HTML媒体。有一个检测轨道添加的示例:

const mediaElem = document.querySelector("video");
mediaElem.audioTracks.onaddtrack = function(event) {
    audioTrackAdded(event.track);
}

我的HTML代码如下:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Multimedia</title>
</head>

<body>

    <video controls loop muted poster="images/poster.png"
        width="400" height="400">
        <source src="videos/rabbit320.webm" type="video/webm">
        <source src="videos/rabbit320.mp4" type="video/mp4">
        <p>
            Your browser doesn't support HTML5 audio. Here is a <a href="audio/viper.mp3">link to the audio</a> instead.
        </p>
    </video>

    <script src="scripts/script.js"></script>
</body>

</html>

和JS代码( script.js ):

var videoElem = document.querySelector('video');
videoElem.audioTracks.onaddtrack = function (event) {
    console.log(event.track);
};

在控制台中,我收到此错误:

未捕获的TypeError:无法在script.js:2上设置未定义的属性“ onaddtrack”

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

HTMLMediaElement.audioTracks属性仍位于标志后面,如此处https://caniuse.com/#feat=audiotracks

所述

MDN上的兼容性表可能未正确显示Opera支持的功能。