我正在尝试在chromecast上播放多语言音轨。它播放视频/音频文件,但不会从英语切换到西班牙语。我可以在silverlight播放器中使用相同的文件进行切换。我已经将我的chromecast播放器语言和我的Android手机语言设置为西班牙语。在Google Cast SDK Developer控制台中,我添加了西班牙语作为备用曲目。
我已经使用Google指南中指定的代码添加了我的自定义播放器,但是指南中的代码似乎无法直接复制和粘贴。有人可以指示我为什么protocol.getStreamCount()为0?通话时间是否正确?这是我正在使用的整个播放器,特别注意window.changeLanguage = function()...和changeLanguage调用。非常感谢任何帮助!
<html>
<head>
<title>Test Player</title>
<script type="text/javascript" src="//www.gstatic.com/cast/sdk/libs/receiver/2.0.0/cast_receiver.js"></script>
<script type="text/javascript" src="//www.gstatic.com/cast/sdk/libs/mediaplayer/1.0.0/media_player.js"></script>
<link rel="stylesheet" type="text/css" href="chromecast.css" />
</head>
<body>
<video id='vid' />
<script type="text/javascript">
if (window.location.href.indexOf('Debug=true') != -1) {
cast.receiver.logger.setLevelValue(cast.receiver.LoggerLevel.DEBUG);
cast.player.api.setLoggerLevel(cast.player.api.LoggerLevel.DEBUG);
}
var mediaElement = document.getElementById('vid');
var protocol = null;
// Create the media manager. This will handle all media messages by default.
window.mediaManager = new cast.receiver.MediaManager(mediaElement);
window.defaultOnLoad = mediaManager.onLoad;
mediaManager.onLoad = function (event) {
if (window.player !== null) {
player.unload(); // Must unload before starting again.
window.player = null;
}
if (event.data['media'] && event.data['media']['contentId']) {
console.log('Starting media application');
var url = event.data['media']['contentId'];
window.host = new cast.player.api.Host({'mediaElement':mediaElement, 'url':url});
var ext = url.substring(url.lastIndexOf('.'), url.length);
var initStart = event.data['media']['currentTime'] || 0;
var autoplay = event.data['autoplay'] || true;
mediaElement.autoplay = autoplay; // Make sure autoplay get's set
if (url.lastIndexOf('.m3u8') >= 0)
{
// HTTP Live Streaming
protocol = cast.player.api.CreateHlsStreamingProtocol(host);
}
else if (url.lastIndexOf('.mpd') >= 0)
{
// MPEG-DASH
protocol = cast.player.api.CreateDashStreamingProtocol(host);
}
else if (url.indexOf('.ism/') >= 0)
{
// Smooth Streaming
protocol = cast.player.api.CreateSmoothStreamingProtocol(host);
}
// How to override a method in Host. I know that it's safe to just provide this
// method.
host.onError = function(errorCode) {
console.log("Fatal Error - " + errorCode);
if (window.player) {
window.player.unload();
window.player = null;
}
};
console.log("we have protocol " + ext);
if (protocol !== null) {
console.log("Starting Media Player Library");
window.player = new cast.player.api.Player(host);
window.player.load(protocol, initStart);
changeLanguage();
}
else {
window.defaultOnLoad(event); // do the default process
}
}
}
window.changeLanguage = function() {
var currentLanguage = null;
var streamCount = protocol.getStreamCount();
console.log("streamCount: " + streamCount);
var streamInfo;
for (var i = 0; i < streamCount; i++) {
console.log("isStreamEnabled " + i + "? " + protocol.isStreamEnabled(i));
if (protocol.isStreamEnabled(i)) {
streamInfo = protocol.getStreamInfo(i);
console.log("streamInfo isAudio? " + streamInfo.mimeType.indexOf('audio'));
if (streamInfo.mimeType.indexOf('audio') === 0) {
if (streamInfo.language) {
console.log("streamInfo.language: " + streamInfo.language);
currentLanguage = i;
break;
}
}
}
}
if (currentLanguage === null) {
currentLanguage = 0;
}
i = currentLanguage + 1;
console.log("i: " + i);
console.log("currentLanguage: " + currentLanguage);
while (i !== currentLanguage) {
if (i === streamCount) {
console.log("resetting i to 0");
i = 0;
}
streamInfo = protocol.getStreamInfo(i);
if (streamInfo.mimeType.indexOf('audio') === 0) {
protocol.enableStream(i, true);
protocol.enableStream(currentLanguage, false);
console.log("Enabling: " + i);
console.log("Disabling: " + currentLanguage);
break;
}
i++;
}
if (i !== currentLanguage) {
console.log("reloading window player" + window.player);
window.player.reload();
}
};
window.player = null;
console.log('Application is ready, starting system');
window.castReceiverManager = cast.receiver.CastReceiverManager.getInstance();
castReceiverManager.start();
</script>
</body>
</html>
Chromecast控制台:
应用程序准备就绪,启动系统
启动媒体应用
我们有协议.ism / manifest
启动Media Player库
streamCount:0
我:1
currentLanguage:0
修改
我进入控制台并发布加载后调用以下命令:
this.protocol.getStreamCount()
2
所以这显然是一个时间问题。一旦流完全加载,我需要调用changeLanguage。我正在调查适当的时间来调用它。我很感激任何帮助,但我会尝试谷歌这个答案,因为我很肯定这是一个时间问题。
答案 0 :(得分:1)
答案是:
host.onManifestReady = function() {
changeLanguage();
};
这将为何时拉出备用音轨提供正确的时间。