启动播放器后,Youtube API调用播放器功能 [英] Youtube API calling player functions after player is initated
问题描述
这是我第一次使用youtube api,我发现很难理解文档。基本上我在页面上有多个视频,当你点击它时,它会弹出一个灯箱并播放正确的视频。所有这一切都很好。
This is my first use of the youtube api and I'm finding it hard understanding the documentation. Basically I have multiple videos on the page, when you click one it pops up in a lightbox and plays the correct video. All of this works fine.
客户现在希望所有视频在开始时自动静音,一旦完成视频,他们希望帧跳到正确的位置。
The client now wants all the videos to be automatically muted on start, and once you finish the video they want the frame to skip to the right place.
如果我使用youtube iframe api,我似乎根本无法更新videoId。以下是我的代码:
If I use the youtube iframe api i can't seem to be able to update the videoId at all. Below is my code:
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height: '323',
width: '576',
videoId: 'Fy7Li0dMRSY',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
function onPlayerReady(event) {
event.target.mute(); // this does not do anything
event.target.playVideo();
}
var done = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
//setTimeout(stopVideo, 6000);
done = true;
}
}
function stopVideo() {
player.stopVideo();
}
function loadVid(vidID, title){
// loads youtube video with video title & youtube video ID
$('.lightbox .title').html(title);
$('.lightbox iframe').prop('src','https://www.youtube.com/embed/'+vidID+'?rel=0&wmode=transparent&enablejsapi=1');
//player.mute() does not work here
showLightbox();
}
我的问题是尝试调用播放器功能在loadVid函数内部静音,它抛出一个错误(任何函数抛出相同的错误),我无法弄清楚如何使用播放器等功能。
My problem is trying to call the player function to mute inside the loadVid function, it throws back an error (any function throws the same error) and I can't figure out how to use functions such as player.
错误是player.stopVideo是不是功能
The error is player.stopVideo is not a function
那么我怎样才能在开始时静音视频?将它添加到onPlayerReady也不起作用。
So how can I mute the video on start? Adding it to onPlayerReady does not work either.
推荐答案
移动event.target.mute();像这样的onPlayerStateChange函数:
Move the event.target.mute(); to the onPlayerStateChange function like this:
var done = false;
function onPlayerStateChange(event) {
event.target.mute();
if (event.data == YT.PlayerState.PLAYING && !done) {
//setTimeout(stopVideo, 6000);
done = true;
}
}
这篇关于启动播放器后,Youtube API调用播放器功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!