检测 video.load 是否成功 - javascript

detect if video.load is successful or not - javascript

如何知道歌曲是否加载成功? 类似于:

video.load({
        success : function() {
            alert("loading successful");
        },
        failure : function() {
            alert("loading not successful");
        } 
    });

我有要播放的歌曲列表,如果突然网络连接有问题,无法播放下一首视频,我想播放已经下载的下一首歌曲..

非常感谢!

您可以使用 html5 视频标签的 error property(但目前似乎只有 IE)。 另一种方法(对于 FireFox)是 HTML 5 Video Error handling in Gecko 2.0 (Firefox 4 / Thunderbird 3.3 / SeaMonkey 2.1).

To detect that all child elements have failed to load, check the value of the media element's networkState attribute. If this is HTMLMediaElement.NETWORK_NO_SOURCE, you know that all the sources failed to load.

希望对您有所帮助!

这取决于您在加载视频时所说的成功。能够完整播放视频是一回事,加载元数据又是另一回事,每个事件都有对应的事件。与失败相同:您会考虑很长时间缓冲错误吗?

请参阅此处 HTMLMediaElement 触发的事件列表: https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement

至于监听这些事件,这段代码可能会有帮助:

var v = document.createElement("VIDEO");

v.addEventListener("error", function (e) {
    console.log("Error while loading the video");
});

v.addEventListener("loadeddata", function () {
    console.log("Video has started loading successfully!");
});

v.src = "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4";