我正在研究适用于Android的Google Cast Sender应用程序教程,但在连接到Cast CustomReceiver
时遇到了麻烦。
我使用以下ConnectionCallbacks
实现:
private class ConnectionCallbacks implements GoogleApiClient.ConnectionCallbacks {
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "ConnectionCallbacks.onConnected");
LaunchOptions options = new LaunchOptions.Builder()
.setRelaunchIfRunning(false)
.build();
Cast.CastApi
.launchApplication(mApiClient, getString(R.string.google_cast_key), options)
.setResultCallback(new ResultCallback<Cast.ApplicationConnectionResult>() {
@Override
public void onResult(Cast.ApplicationConnectionResult result) {
Status status = result.getStatus();
if (status.isSuccess()) {
Log.i(TAG, "Success!!! " + result.getApplicationStatus());
} else {
Log.e(TAG, "No success!!! " + result.getApplicationStatus());
}
}
});
}
@Override
public void onConnectionSuspended(int i) {
Log.i(TAG, "ConnectionCallbacks.onConnectionSuspended: " + i);
}
}
和CustomReceiver代码:
<script>
window.castReceiverManager = cast.receiver.CastReceiverManager.getInstance();
var appConfig = new cast.receiver.CastReceiverManager.Config();
appConfig.statusText = 'Ready to play';
// 100 minutes for testing, use default 10sec in prod by not setting this value
appConfig.maxInactivity = 6000;
window.castReceiverManager.start();
console.log('Starting Receiver Manager');
document.getElementById("message").innerHTML='Starting Receiver Manager';
</script>
当我从对话框中选择chromecast时,我的自定义接收器应用程序将在chromecast上启动,并且该应用程序日志
11-05 22:38:00.470: I/MainActivity(8955): ConnectionCallbacks.onConnected
但是,它永远不会调用
onResult
或onConnectionSuspended
的ResultCallback
或ConnectionFailedListener
。我想念什么?添加了日志文件
[ 0.340s] [cast.receiver.CastReceiverManager] Dispatching CastReceiverManager system ready event
desorona_cast_receiver.html:45 Received Ready event: {"id":"E9198EB3","name":"DESORONA","sessionId":"93EB8C53-4589-4D49-B578-E46D289B57F9","namespaces":[],"launchingSenderId":"40:com.example.test-145"}
cast_receiver.js:40 [ 0.353s] [cast.receiver.IpcChannel] IPC message sent: {"namespace":"urn:x-cast:com.google.cast.system","senderId":"SystemSender","data":"{\"type\":\"setappstate\",\"statusText\":\"Application status is ready...\"}"}
cast_receiver.js:40 [ 0.373s] [cast.receiver.IpcChannel] Received message: {"data":"{\"level\":1.0,\"muted\":false,\"type\":\"volumechanged\"}","namespace":"urn:x-cast:com.google.cast.system","senderId":"SystemSender"}
cast_receiver.js:40 [ 0.375s] [cast.receiver.CastMessageBus] Dispatching CastMessageBus message
cast_receiver.js:40 [ 0.378s] [cast.receiver.CastReceiverManager] Dispatching system volume changed event [1, false]
cast_receiver.js:40 [ 0.382s] [cast.receiver.IpcChannel] Received message: {"data":"{\"type\":\"visibilitychanged\",\"visible\":true}","namespace":"urn:x-cast:com.google.cast.system","senderId":"SystemSender"}
cast_receiver.js:40 [ 0.385s] [cast.receiver.CastMessageBus] Dispatching CastMessageBus message
cast_receiver.js:40 [ 0.388s] [cast.receiver.CastReceiverManager] Dispatching visibility changed event true
cast_receiver.js:40 [ 0.392s] [cast.receiver.IpcChannel] Received message: {"data":"{\"standby\":false,\"type\":\"standbychanged\"}","namespace":"urn:x-cast:com.google.cast.system","senderId":"SystemSender"}
cast_receiver.js:40 [ 0.393s] [cast.receiver.CastMessageBus] Dispatching CastMessageBus message
cast_receiver.js:40 [ 0.395s] [cast.receiver.CastReceiverManager] Dispatching standby changed event false
最佳答案
我解决了我的问题。不是发送者,而是接收者。 Google并未声明,使用CustomReceivers时需要注册MessageBus。将发送方连接到接收方的最小ReceiverCode必须为:
var namespace = 'urn:x-cast:com.denis_loh.desorona'
window.onload = function() {
cast.receiver.logger.setLevelValue(0);
window.castReceiverManager = cast.receiver.CastReceiverManager.getInstance();
// Erstelle MessageBus zum Austauschen von Daten
window.messageBus = window.castReceiverManager.getCastMessageBus(namespace);
// Lege onMessage-Callback für den MessageBus fest.
window.messageBus.onMessage = onMessage;
// Starte den Receiver.
window.castReceiverManager.start({statusText: "Application is starting"});
console.log('Receiver Manager started');
};
function onMessage(event) {
window.messageBus.send(event.senderId, event.data);
}
但是,我还不知道,为什么毕竟需要这样做。