我在windows 8应用程序中嵌入了youtube播放器(html/javascript)。我需要与玩家互动(播放/暂停等)。我在“入门”部分这里中复制/粘贴了示例,但是没有触发onYouTubeIframeAPIReady。我为相同的代码创建本地html文件,并在chrome中启动html文件,但结果相同。当我使用w3schools尝试它页面时,这段代码工作得很好。我找到了一个与youtube播放器这里互动的更好的例子。我在桌面上本地复制了index.js、index.html和index.css。尝试从桌面运行index.html不起作用(基本上没有触发onYouTubeIframeAPIReady )。
我的最终目标是知道视频何时在win8 javascript/html应用程序中播放完毕。我试着让上面的例子首先在当地起作用,但到目前为止没有成功。
谢谢你的帮助!
P.S:关于堆栈溢出没有几个相关的问题(例如:http://stackoverflow.com/questions/12844668/controlling-an-iframe-youtube-player-within-a-windows-8-metro-js-app),但我无法找到答案。此外,由于声誉低下,我无法发表评论:)。
编辑:这里的是我在本地使用的实际HTML:
<!DOCTYPE html>
<html>
<body>
<!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
<div id="player"></div>
<script>
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "//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: 'u1zgFlCw8Aw',
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>发布于 2012-12-10 00:54:25
在您提供的谷歌示例YouTube页面第2节中,请更改以下内容:
tag.src = "//www.youtube.com/iframe_api";对此:
tag.src = "http://www.youtube.com/iframe_api";然后,此页面将在本地工作。另外,请注意,您也可以使用https。
https://stackoverflow.com/questions/13793146
复制相似问题