我正在尝试向ChromeCast的自定义接收器发送消息。我在Android应用程序中使用以下代码将代码发送给接收方;
Cast.CastApi.sendMessage(mApiClient, "urn:x-cast:move", "TEST");
在接收方,我有以下代码;
window.mediaElement = document.getElementById('media');
window.mediaManager = new cast.receiver.MediaManager(window.mediaElement);
window.castReceiverManager = cast.receiver.CastReceiverManager.getInstance();
window.castReceiverManager.start();
window.castReceiverManager.onSenderConnected = function(event) {
//This is called
}
window.customMessageBus = window.castReceiverManager.getCastMessageBus('urn:x-cast:move', cast.receiver.CastMessageBus.MessageType.STRING);
var defaultFunction = window.customMessageBus.onMessage;
window.customMessageBus.onMessage = function(event) {
//This is not called
defaultFunction(event);
};
正如我在代码中指出的那样,'onSenderConnected'被调用,所以我知道它连接到了应用程序。但是当我尝试通过自定义消息总线发送消息时,它并没有给我任何东西。我是android和cast的新手,所以我可能做错了一千件事。任何人都可以帮我解决这个问题吗?
答案 0 :(得分:2)
我自己解决了。我做错了是在添加自定义命名空间之前启动了castReceiverManager。因此,与我在问题中发布的内容相比,接收器的正确代码将是;
window.mediaElement = document.getElementById('media');
window.mediaManager = new cast.receiver.MediaManager(window.mediaElement);
window.castReceiverManager = cast.receiver.CastReceiverManager.getInstance();
//Removed the start here
window.castReceiverManager.onSenderConnected = function(event) {
//OnConnect
}
window.customMessageBus = window.castReceiverManager.getCastMessageBus('urn:x-cast:move', cast.receiver.CastMessageBus.MessageType.STRING);
var defaultFunction = window.customMessageBus.onMessage;
window.customMessageBus.onMessage = function(event) {
//OnMessage
defaultFunction(event);
};
//Start at the end
window.castReceiverManager.start();