gpt4 book ai didi

Chromecast 上的 YouTube iframe api 行为

转载 作者:行者123 更新时间:2023-12-02 00:22:40 26 4
gpt4 key购买 nike

尝试在 Chromecast 上播放 YouTube 视频,不使用 YouTube 接收器,而仅使用 iframe YouTube api。当接收者网址加载到桌面 Chrome 浏览器中时,它可以正常播放,但当相同的网址加载到 Chromecast 上时,我收到消息“此视频当前不可用 - 了解更多”。如果我一直尝试让小部件创建后播放不同的视频,有时它还会显示“视频播放需要 Adob​​e Flash Player”。

该小部件是使用新的 YT.Player 在 onYouTubeIframeAPIReady() 回调中创建的,如文档中所示。也许我很困惑,但我认为 iframe api 是基于 html5 的,而不是基于 flash 的。有没有人成功实现了这一点,或者 Chromecast 不支持这一点,因此出现了奇怪的行为?我也偶然发现了这个http://www.youtube.com/html5

最佳答案

这是我当前在接收器上使用的代码。消息处理(两个方向)时好时坏,我目前正在解决这个问题……但是 iFrame 库的加载、视频的嵌入等都可以正常工作。如果它对您不起作用,我们可以开始调查您的设置可能有何不同。我尝试在可能有帮助的地方添加评论。

<html>
<head>
<script src="https://www.gstatic.com/cast/js/receiver/1.0/cast_receiver.js">
</script>
<script type="text/javascript">
// first create our receiver object and our channel handler
var receiver = new cast.receiver.Receiver('{YOUR_APP_ID}', ['ChromecastYoutube'],"",5);
var ytChannelHandler = new cast.receiver.ChannelHandler('ChromecastYoutube'); // 'using 'ChromecastYoutube' as my dev namespace. Wouldn't really be that in production.
ytChannelHandler.addChannelFactory(receiver.createChannelFactory('ChromecastYoutube'));
ytChannelHandler.addEventListener(
cast.receiver.Channel.EventType.MESSAGE,
onMessage.bind(this)
);

receiver.start();

window.addEventListener('load', function() { // we won't try to load the iframe libraries until the chromecast window is fully loaded.
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 player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '562',
width: '1000',
videoId: 'jLlSqucqXB0',
playerVars: { 'autoplay': 0, 'controls': 0 },
events: {
'onReady': onPlayerReady,
'onPlayerStateChange': onStateChange
}
});
}

function onPlayerReady(event) {
document.getElementById('annotation').innerHTML="We're ready to go";
}

function onStateChange(event) {
switch (event.data) {
case YT.PlayerState.ENDED:
// TODO let sender know we're done, then close casting
break;
case YT.PlayerState.PLAYING:
// TODO let sender know we're playing
break;
case YT.PlayerState.PAUSED:
// TODO let sender know we're paused
break;
}
}

function onMessage(event) { // currently, any one of these will work, but subsequent ones seem to falter. Investigating...
ytBindings={"playVideo":player.playVideo(),"pauseVideo":player.pauseVideo(),"stopVideo":player.stopVideo(),"getStatus":player.getPlayerState()}
ytBindings[event.message];
}


</script>
<style>
#wrapper {
width: 1000px;
margin: 10px auto;
text-align: center;
}
#annotation {
color: #ffffcc;
font-size: 200%;
margin-top:25px;
}
</style>
</head>
<body>
<div id="wrapper">
<div id="player"></div>
<div id="annotation"></div>
</div>
</body>
</html>

关于Chromecast 上的 YouTube iframe api 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18582694/

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