gpt4 book ai didi

javascript - YouTube javascript 未返回正确的错误代码(所有内容均为错误 0)

转载 作者:行者123 更新时间:2023-12-04 22:05:58 24 4
gpt4 key购买 nike

我似乎无法让我的 YouTube javascript 给我正确的视频错误。例如,下面的代码应该给我错误代码 150,但它给了我 0。

在 HTML 上嵌入代码

<object width='710' height='400' style='z-index: 1;'>
<param name='movie' value='https://www.youtube.com/v/Ze8bj80qL_I?version=3&enablejsapi=1&autoplay=1&rel=0&wmode=transparent'></param>
<param name='allowFullScreen' value='true'></param>
<param name='allowScriptAccess' value='always'></param>
<param name='wmode' value='transparent'></param>
<embed src='https://www.youtube.com/v/Ze8bj80qL_I?version=3&enablejsapi=1&autoplay=1&rel=0&wmode=transparent' type='application/x-shockwave-flash' allowfullscreen='true' allowScriptAccess='always' width='710' height='400' id='myytplayer' wmode='transparent'></embed>
</object>

Javascript:

function onPlayerError(event) {
alert(event);
}
function onYouTubePlayerReady(playerId) {
ytplayer = document.getElementById("myytplayer");
ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
ytplayer.addEventListener("onError", "onPlayerError");
$(ytplayer).attr({
"src" : url.replace('?rel=0', '')+"?wmode=transparent",
"wmode" : "Opaque"
});
}

最佳答案

我不知道为什么,但只有使用 YouTube IFrame API 才能得到正确的错误代码使用 HTML5 播放器。

HTML:

<div id="player"></div>

JavaScript:

var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

function onPlayerError(event) {
alert("Error code " + event.data);
}

function onYouTubeIframeAPIReady() {
console.log("start");
var player;
player = new YT.Player('player', {
videoId: 'Ze8bj80qL_I',
playerVars: {
'autoplay': 1,
'controls': 0,
'html5': 1
},
events: {
'onError': onPlayerError
}
});
}

错误代码为100:

The video requested was not found. This error occurs when a video has been removed (for any reason) or has been marked as private.

Here is the jsfiddle.在 GC30 中测试。

关于javascript - YouTube javascript 未返回正确的错误代码(所有内容均为错误 0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19614038/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com