Youtube api 无法在Windows 8.1 winjs库中播放youtube视频
我正在尝试在Windows8.1应用程序中播放youtube视频,代码如下Youtube api 无法在Windows 8.1 winjs库中播放youtube视频,youtube-api,winjs,youtube-javascript-api,Youtube Api,Winjs,Youtube Javascript Api,我正在尝试在Windows8.1应用程序中播放youtube视频,代码如下 var videoPlayer = document.getElementById("videoPlayer"); var content = '<iframe width="480" height="270" src="http://www.youtube.com/embed/XGSy3_Czz8k?rel=0" frameborder="0" allowfullscreen></
var videoPlayer = document.getElementById("videoPlayer");
var content = '<iframe width="480" height="270" src="http://www.youtube.com/embed/XGSy3_Czz8k?rel=0" frameborder="0" allowfullscreen></iframe>';
WinJS.Utilities.setInnerHTMLUnsafe(videoPlayer, content);
var videoPlayer=document.getElementById(“videoPlayer”);
var内容=“”;
setInnerHTMLUnsafe(视频播放器,内容);
视频播放器是视频播放器出现的div的名称,但我无法播放视频。
我还尝试使用来自的示例代码
但效果不太好。请任何人帮我做这个
提前感谢我设法让这个场景为我工作,但我不得不克服一些困难才能让它工作 首先,为了加载与iframe通信所需的外部脚本,我必须在播放器自己的外部iframe中对其进行沙箱处理,该外部iframe可以在web上下文中运行:
<div class="playerContainer">
<iframe class="playerFrame" src="ms-appx-web:///pages/video/iframe.html" style="visibility: hidden" scrolling="no"></iframe>
</div>
我的iframe.html非常简单:
<!DOCTYPE html>
<html>
<head>
<title>Video Player Iframe</title>
<link href="/pages/video/iframe.css" rel="stylesheet" />
<script src="/pages/video/iframe.js"></script>
</head>
<body>
<iframe id="player"
src="https://www.youtube.com/embed/?enablejsapi=1"
frameborder="0"
style="visibility: hidden"></iframe>
</body>
</html>
从上面可以看到,我使用iframe的postMessage告诉包装iframe加载我想要的视频ID:
iframe.addEventListener("load", function () {
iframe.style.visibility = 'visible';
if (iframe.contentWindow) {
iframe.contentWindow.postMessage({ op: "load", args: { videoId: video.id} }, "*");
}
});
希望这能帮助一些人。YouTube IFrame API非常挑剔,在以任何其他方式加载时都不适合我。感谢您的回复,但我也面临同样的问题,根本没有显示。我想知道是否有任何api可以帮助我在MediaPlayer框架中播放youtube视频。很抱歉,它没有为您显示。您在JS控制台中是否有任何错误?另外,YouTube永远不会让你获得原始视频流,所以我认为你不能使用播放器框架。我得到了错误<代码>无法加载类ID为“{D27CDB6E-AE6D-11CF-96B8-444553540000}”的ActiveX插件。应用程序无法加载ActiveX控件。
(function () {
"use strict";
window.addEventListener('message', function (e) {
if (e.data.op === 'load') {
loadVideo(e.data.args)
} else if (e.data.op === 'resize') {
resize(e.data.args);
} else if (e.data.op === 'destroy') {
destroy();
}
});
var player;
var videoId;
function loadVideo(args) {
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var playerEl = document.getElementById('player');
playerEl.height = window.innerHeight;
playerEl.width = window.innerWidth;
videoId = args.videoId;
window.onYouTubeIframeAPIReady = function onYouTubeIframeAPIReady() {
playerEl.style.visibility = 'visible';
player = new YT.Player('player', {
events: {
'onReady': onPlayerReady
}
});
}
}
function resize(args) {
player && player.setSize(window.innerWidth, window.innerHeight);
}
function destroy() {
player && player.destroy();
player = null;
}
function onPlayerReady(event) {
event.target.loadVideoById(videoId);
}
})();
iframe.addEventListener("load", function () {
iframe.style.visibility = 'visible';
if (iframe.contentWindow) {
iframe.contentWindow.postMessage({ op: "load", args: { videoId: video.id} }, "*");
}
});