我正在尝试查看是否可以在Android发送者的Chromecast视频之前/期间播放广告。我知道我可以处理接收器端的逻辑并切换视频src来播放广告,但是如何在发送器应用上暂停控制器以便在接收器上更改视频src时它不会崩溃?我一直在浏览文档,但我似乎无法找到关于这个主题的任何内容。
感谢您的帮助
答案 0 :(得分:1)
您有几种不同的选择。我认为对你的情况更合适的一种方法是让你的接收器在发生变化时向所有连接的发送者广播接收方的变化,以便发送者可以自己调整/更新;更改可能是玩家状态和元数据更改。例如,在正常情况下,发件人A可以开始播放电影A.然后在播放时,也连接的发件人B可以开始播放电影B.然后发件人A将收到媒体状态和元数据更新通知媒体有变化,所以它可以采取适当的行动,无论它是什么。你的情况并没有那么不同;发件人方面的处理可能会根据您的要求而有所不同。
答案 1 :(得分:0)
我一直在使用从接收方到发送方的自定义消息,以通知发送方接收方的任何状态变化,效果很好。以下是处理从发件人到收件人的各种邮件的示例代码段 - 我的应用程序是卡拉OK播放列表,用于排队播放视频 - 它基于CastVideoPlaylist示例应用程序)
在发送方,我只是注册一个消息监听器来相应地更新UI。
var playlistMessageBus = castReceiverManager.getCastMessageBus(
namespace, cast.receiver.CastMessageBus.MessageType.JSON);
// Create a message handler for the custome namespace channel
playlistMessageBus.onMessage = function(event) {
console.log('Playlist message: ' + JSON.stringify(event));
// Handle the ADD command from the sender
// Other commands could also be supported
switch (event.data.command) {
case 'ADD':
if (playlist == null) {
playlist = [];
}
console.log("Adding " + event.data.playlistItem.contentId + " to playlist");
playlist.push(event.data.playlistItem);
break;
case 'DELETE':
// delete the selected item from playlist
{
var code = 0;
if (playlist == null) {
code = 0;
}
else {
// find the listItem title, then delete it
var index;
for (index = 0; index < playlist.length; index++) {
if (playlist[index].title == event.data.playlistItem.title) {
console.log('Found ' + event.data.playlistItem.title + 'in playlist');
break;
}
}
if (index < playlist.length) {
playlist.splice(index,1);
code = 1;
}
}
var message = {
commandResult: 'CMD_RESULT',
responseCode: code
};
playlistMessageBus.send(event.senderId, JSON.stringify(message));
}
break;
case 'MOVE_TOP':
{
// move the selected listItem to top of the playlist
var code = 0;
if (playlist == null) {
code = 0;
}
else if (playlist.length == 1) {
code = 1;
}
else {
// find the listItem title, then move it to the top
var index;
for (index = 0; index < playlist.length; index++) {
if (playlist[index].title == event.data.playlistItem.title) {
console.log('Found ' + event.data.playlistItem.title + 'in playlist');
break;
}
}
if (index < playlist.length) {
playlist.splice(index,1);
playlist.unshift(event.data.playlistItem);
code = 1;
}
}
var message = {
commandResult: 'CMD_RESULT',
responseCode: code
};
playlistMessageBus.send(event.senderId, JSON.stringify(message));
}
break;
case 'QUERY':
switch (event.data.category) {
case 'PLAY_STATUS':
{
console.log("Sending play status back to sender app");
// inform all senders on the CastMessageBus of the incoming message event
// sender message listener will be invoked
var playState = (inPlay) ? 'PLAYING' : 'STOPPED';
var message = {
playstatus: 'PLAY_STATUS',
state: playState
};
playlistMessageBus.send(event.senderId, JSON.stringify(message));
}
break;
case 'SELECTED_MEDIA':
{
// return the current select media
var listItem = null;
if (curLoadedPlaylistItem != null) {
listItem = {
title: curLoadedPlaylistItem.title,
contentId : curLoadedPlaylistItem.contentID,
image: curLoadedPlaylistItem.image
};
}
var message = {
mediastatus: 'MEDIA_STATUS',
playlistItem: listItem
};
playlistMessageBus.send(event.senderId, JSON.stringify(message));
}
break;
case 'PLAYLIST':
{
// return the playlist
var message = {
playliststatus: 'PLAYLIST',
playlist: playlist
};
playlistMessageBus.send(event.senderId, JSON.stringify(message));
}
break;
default:
console.log("Unrecognized category from sender: " + event.data.category);
break;
}
break;
default:
console.log("Unrecognized command from sender: " + event.data.command);
break;
}
};