0
votes

I'm trying to see if it's possible to play ads before/during a video on Chromecast from an Android sender. I understand that I can handle the logic on the receiver side and switch video src to play an ad, but how would I go about pausing the controller on the sender app so that it doesn't crash when video src is changed on the receiver? I've been looking through the docs but I really can't seem to find anything on this topic.

Thanks for the help

2

2 Answers

1
votes

You have a couple of different options. I think the more appropriate one for your case is to have your receiver broadcast the changes on your receiver side to all the connected senders when a change happens so senders can adjust/update themselves; changes could be player status and metadata changes. For example, in a normal case, Sender A can start playback of movie A. Then while that is playing, Sender B, which is also connected, can start playing movie B. Then the Sender A will receive a media status and metadata updates informing it that the media has changes, so it can take the appropriate action, whatever it is. Your situation is not that different; handling on the sender side might be different based on your requirements.

0
votes

I have been using custom messages from the receiver to the sender to notify the sender of any status change on the receiver side with great results. Here is an example code snippet to handle various messages from the sender to the receiver -- My app is a Karaoke playlist that queues videos for playing -- it was based on CastVideoPlaylist sample app)

On the sender side, I just register a message listener to update the UI accordingly.

           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;
            }
        };