我有这个自定义YouTube嵌入一些整洁的小工具,它工作得很好,但有一个问题。控制台中有许多错误,例如:
Uncaught TypeError: Cannot read property 'getVolume' of undefined
index.html:189 Uncaught TypeError: player.getVolume is not a function
index.html:179 Uncaught TypeError: player.getCurrentTime is not a function
index.html:183 Uncaught TypeError: player.getCurrentTime is not a function
我不知道是什么可能导致这些错误,我试过做很多事情的代码,如移动和重新分配变量,我只是无法清除它们,有什么帮助吗?
CODEPEN: http://codepen.io/mistkaes/pen/MwONzK
尝试使用玩家的onReady
方法来射击它的间隔,所以你将确保player
变量已经定义。
function onYouTubeIframeAPIReady() {
var rangerGo;
var player = new YT.Player('player', {
height: '282',
width: '502',
videoId: 'QExOaGT_ids',
playerVars: {
'controls': 0,
'showinfo': 0,
'iv_load_policy': 3,
'rel': 0,
},
events: {
onReady: function(){
setInterval(function() {
$("#content").text("video_time: " + player.getCurrentTime().toString().toHHMMSS());
}, 250);
rangerGo = setInterval(function() {
$("#range").slider("value", player.getCurrentTime());
$("#range").slider("option", "max", player.getDuration());
}, 250);
setInterval(function() {
// VOLUME CONTROLS
$("#volume-amount").text("volume: " + player.getVolume() + "%");
player.setVolume($("#volume-range").slider("value"));
}, 1);
$("#range").slider({
range: "min",
start: function(event, ui) {
player.pauseVideo();
clearInterval(rangerGo);
},
stop: function(event, ui) {
player.seekTo(ui.value, true);
player.playVideo();
rangerGo = setInterval(function() {
$("#range").slider("value", player.getCurrentTime());
$("#range").slider("option", "max", player.getDuration());
}, 250);
}
});
}
}
});
}