我正在使用适用于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
的{{1}}或onConnectionSuspended
以及ResultCallback
。我错过了什么?
添加了日志文件
ConnectionFailedListener
答案 0 :(得分:3)
我解决了我的问题。它不是发送者,而是接收者。 Google未声明,在使用CustomReceivers时需要注册MessageBus。获取连接到Receiver的Sender的最小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);
}
然而,我还不知道,为什么这毕竟是必需的。