我在html中有以下代码:
<div class="flex-video widescreen youtube">
<iframe id="ytplayer" type="text/html" width="640" height="360" src="https://www.youtube.com/embed/{{Video.v_id}}?autoplay=1&enablejsapi=1&autohide=1" frameborder="0" allowfullscreen> </iframe>
</div>这个Javascript中的代码:
function onYouTubePlayerReady(playerId) {
console.log("player ready");
ytplayer = document.getElementById("ytplayer");
}当我输入Firefox控制台时,如下所示:
ytplayer.playVideo();我得到了非函数错误:
TypeError: ytplayer.playVideo不是一个函数
此外,console.log("player ready");根本不打印。
有人能帮我吗?
我想用javascript来控制播放器,而不是用IFrame api。我希望视频播放器在html5。
编辑:我启用了jspapi (enablejsapi=1)
发布于 2015-09-15 13:50:34
对我有用的是通过文件和JS添加youtube,然后使用onYoutubePlayerAPIReady事件,如下所示:
window.onYouTubePlayerAPIReady = function(){
var player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'M7lc1UVf-VE',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}发布于 2014-10-30 15:44:58
如果您遵循YouTube API播放器的文档
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div id="player"></div>
</body>
</html>JS
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'l-gQLqv9f4o',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.playVideo();
}
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING) {
}
}
function stopVideo() {
player.stopVideo();
}现场演示
https://stackoverflow.com/questions/26656836
复制相似问题