gpt4 book ai didi

javascript - 在 Windows 8 应用程序中使用 Youtube Player API

转载 作者:行者123 更新时间:2023-11-30 05:52:37 25 4
gpt4 key购买 nike

我在 Windows 8 应用程序 (html/javascript) 中嵌入了 youtube 播放器。我需要与玩家互动(播放/暂停等)。我在入门部分复制/粘贴示例 here但未触发 onYouTubeIframeAPIReady。我为相同的代码创建了本地 html 文件,并在 chrome 中启动了 html 文件,但结果相同。当我使用 w3schools 尝试页面时,这段代码工作正常。我找到了与 youtube 播放器交互的更好示例 here .我在桌面上本地复制了 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>

最佳答案

Google Sample YouTube Page 的第 2 节中你提供,改变这个:

tag.src = "//www.youtube.com/iframe_api";

为此:

tag.src = "http://www.youtube.com/iframe_api";

然后,该页面将在本地工作。另请注意,您也可以使用 https

关于javascript - 在 Windows 8 应用程序中使用 Youtube Player API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13793146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com