尝试在Chromecast上播放YouTube视频,不使用YouTube接收器,只使用iframe YouTube api。当接收器url加载到桌面chrome浏览器中时,它可以播放,但当相同的url加载到Chromecast上时,我会收到消息"此视频当前不可用-了解更多"。如果我一直试图让这个小部件在创建后播放不同的视频,它有时还会说"视频播放需要Adobe Flash Player"。
小部件是在onYouTubeIframeAPIReady()回调中使用新的YT.Player创建的,如文档所示。也许我很困惑,但我认为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固件16041修复了,如果在Chromecast设置过程中正确设置了国家代码。查看此处的最新评论https://code.google.com/p/gdata-issues/issues/detail?id=5923
老答案:以下是问题的根本原因:https://support.google.com/chromecast/answer/2995235?hl=en某些视频可能无法使用Chromecast播放。私人视频、直播内容和未经批准用于移动播放的视频将无法使用Chromecast希望这将改变,杀死一个主要的Chromecast用例。
我也遇到了这个问题,发现原因是我在视频开始前使用html5音频播放一些音效。删除音效后,youtube播放器又可以正常工作了。