我一直在尝试使用Google的YouTube API暂停和播放iframe,但是我没有成功。我是Java脚本的新手,正在将其用于大学模块。我看过以前的帖子,但没有运气。我也使用了官方文档,再次没有运气。我在播放中添加了两个按钮,然后停止,但是它们都不起作用。任何建议将不胜感激,谢谢!
<!DOCTYPE html>
<html>
<body>
<button id="play" type="button"> Play </button>
<button id="stop" type="button"> Stop </button>
<iframe id="player" type="text/html" width="640" height="390"
src="http://www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1&origin=http://example.com"
frameborder="0"></iframe>
<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) {
document.getElementById("play").onclick"playVideo()";
}
// 5. The API calls this function when the players 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) {
document.getElementById("stop").onclick"stopVideo()";
}
function stopVideo() {
player.stopVideo();
}
function playVideo() {
player.playVideo();
}
</script>
</body>
</html>