基本上,我设法创建了一个网站,使用youtube视频API搜索视频。
我想现在添加一个youtubevideo播放器(iframe?),并且可能能够从文本输入中对视频(url)进行排队。
我的下一个问题,我在互联网上找到了使用iframe所需的代码, 但是脚本都在html页面中,我想知道是否有可能将代码放在我的JS页面中..我试过了,但是没有工作......我认为将语法放在&在我的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>
答案 0 :(得分:0)
我尝试了以下方式;
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
}