gpt4 book ai didi

javascript - 如何正确使用SWFObject

转载 作者:行者123 更新时间:2023-11-29 10:50:35 26 4
gpt4 key购买 nike

我正在使用 SWFObject 将 YouTube 视频嵌入到我们的网站中。一页中有很多视频链接,每个链接清除一个包装器 div,然后使用以下代码加载一个新的嵌入到其中:

$('a.video-link').each(function () {
$(this).on('click', function(e) {
e.preventDefault();

if ($('#video-wrap').has('object').length == 0) {
var params = { allowScriptAccess: 'always', allowFullScreen: 'true' };
var atts = { id: 'ytapiplayer' };
swfobject.embedSWF($(this).attr('href'), 'ytapiplayer', '1000', '562', '8', null, null, params, atts);
$('#video-wrap').show();
} else {
$('#video-wrap').find('object').remove();
$(this).trigger('click');
}
});
});

这些是我为每个嵌入使用的 Youtube 链接:

http://www.youtube.com/v/{Youtube ID}?hl=en_US&rel=0&hd=1&border=0&version=3&fs=1&autoplay=1&autohide=1&enablejsapi=1&playerapiid=ytapiplayer

然后,这是 onYouTubePlayerReady() 事件处理程序:

function onYouTubePlayerReady(id) {
console.log('youtube player is ready.');
var ytplayer = document.getElementById('ytapiplayer');
ytplayer.addEventListener('onStateChange', 'onYouTubePlayerStateChange');
ytplayer.addEventListener('onError', 'onYouTubePlayerError');
}

所有视频都可以正常加载,但永远不会触发 onYouTubePlayerReady!

我已经尝试了来自 here 的解决方案和 here但没有任何效果:(

请帮我解决这个问题。 最终目标是让 Youtube API 正常工作。

谢谢。

编辑:我试着玩弄代码,确保所有名称都正确,分成不同的脚本标签和/或 .js 文件,在开头加载它,在 document.ready() 中,仍然,onYouTubePlayerReady 不是射击。 你怎么看?

最佳答案

这是工作代码:

在每个视频链接上执行 SWFObject:

$('a.video-link').on('click', function(e) {
e.preventDefault();
// SWFObjects loads a video object into div with ID ytapiplayer.
// If the wrapper div already contains a video we need to remove it first:
if ($('#video-wrap').has('object').length == 0) {
var params = { allowScriptAccess: 'always', allowFullScreen: 'true' };
var atts = { id: 'ytapiplayer' };
swfobject.embedSWF($(this).attr('href'), 'ytapiplayer', '1000', '562', '8', null, null, params, atts);

$('#video-wrap').show();
} else {
$('#video-wrap').find('object').remove();
$(this).trigger('click');
}
});

带有 API 值的 YouTube 链接:

http://www.youtube.com/v/' + data.YoutubeLink + '?hl=en_US&rel=0&hd=1&border=0&version=3&fs=1&autoplay=1&autohide=1&enablejsapi=1&playerapiid=ytapiplayer

还有 SWFObject 事件处理程序 我将这段代码放在一个单独的 .js 文件中,在执行 SWFObject 的代码之前加载。我不知道是否有必要,但它仍然有效:

function onYouTubePlayerReady(id) {
// We need the actual DOM element for this, if we want to use more advanced API.
// This is because addEventListener activates the API.
var ytplayer = $('#ytapiplayer').get(0);
ytplayer.addEventListener('onStateChange', 'onYouTubePlayerStateChange'); // onYouTubePlayerStateChange(newState)
ytplayer.addEventListener('onError', 'onYouTubePlayerError'); // onYouTubePlayerError(errorCode)
}

关于javascript - 如何正确使用SWFObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10930011/

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