基本上,我alrdy设法创建了一个网站,以使用youtube video API搜索视频。
我现在想添加一个youtubevideo播放器(iframe?),并且可能能够将来自文本输入的视频(网址)放入队列。
我的下一个问题是,我在互联网上发现了使用iframe所需的代码,
但是脚本全部在html页面中,我想知道是否可以将代码放置在我的JS页面中。我尝试了一下,但是不起作用...我以为将语法放在&中文件会做到这一点...
有人可以解释将代码放在我的js文件中时如何使它工作吗?
我知道这可能是一个愚蠢的问题...
非常感谢!
<html>
<body>
<div id="player"></div>
<script>
// 2. This code loads the IFrame Player API code asynchronously.
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: 'M7lc1UVf-VE',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.playVideo();
}
// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
var done = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !done) {
setTimeout(stopVideo, 6000);
done = true;
}
}
function stopVideo() {
player.stopVideo();
}
</script>
</body>
</html>
最佳答案
我按以下方式尝试过;
in html:
<script src="js/app.js"></script>
in js;
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
..... etc
}
关于javascript - Javascript播放YouTube API(+排队视频),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41214047/