gpt4 book ai didi

javascript - Browserify 破坏了 Youtube iFrame API

转载 作者:行者123 更新时间:2023-11-30 12:26:33 25 4
gpt4 key购买 nike

当我浏览获取和应用 Youtube iFrame API 的脚本时,onYoutubeIframeAPIReady() 不会运行,请求的视频也不会加载。有没有解决的办法?我做错了什么吗?

这是我的脚本:

var Command = require('../model/command.js');
var Control = require('../model/control.js');
var User = require('../model/user.js');

var socket = io();

var executions = {};
executions[1] = function() { player.playVideo(); };
executions[2] = function() { player.pauseVideo(); };

var control = Control();

var now = function() { return new Date().getTime() / 1000; };

var user = User();

var update = function() {
control.room = $('#name').text();

if (user.last) {
if (user.command.date > user.last) {
player.seekTo(user.command.time, true);
executions[user.command.state]();
control.last = user.command.date;
}
} else {
player.seekTo(user.time, true);
executions[user.state]();
control.last = now();
}

control.time = player.getCurrentTime();
control.state = player.getPlayerState();

socket.emit('control', control);
};

var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
videoId: 'M7lc1UVf-VE',
playerVars: {
'controls': 1,
'rel': 1,
},
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}

function onPlayerReady(event) {
$('#youtube-controls').show();
}

function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.Playing) {}
}

$('#youtube-controls').hide();

// Control
$('#pause').click(function() {
player.pauseVideo();
control.command = Command(2, player.getCurrentTime());
});

$('#play').click(function() {
player.playVideo();
control.command = Command(1, player.getCurrentTime());
});

// Input
socket.on('update', function(message) { user = message; });

// Output
setInterval(update, 1000);

最佳答案

onYouTubeIframeAPIReady 函数必须位于全局范围内,因此必须在 window 对象上定义。

因此,在您使用 browserify 的情况下,您需要在 global 对象上定义它:

global.onYouTubeIframeAPIReady = function() {
player = new YT.Player('player', {
videoId: 'M7lc1UVf-VE',
playerVars: {
'controls': 1,
'rel': 1,
},
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}

关于javascript - Browserify 破坏了 Youtube iFrame API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29183142/

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