Google Cast Android Sender:未调用 ResultCallback

Posted

技术标签:

【中文标题】Google Cast Android Sender:未调用 ResultCallback【英文标题】:Google Cast Android Sender: ResultCallback not called 【发布时间】:2016-02-06 22:24:38 【问题描述】:

我正在学习适用于 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

但是,它从不调用ResultCallbackConnectionFailedListeneronResultonConnectionSuspended。我错过了什么?

添加的日志文件

 [  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 教程:developers.google.com/cast/docs/custom_receiver 与 Hello Text 示例应用的组合 【参考方案1】:

我解决了我的问题。不是发送者,而是接收者。 Google 没有声明,在使用 CustomReceivers 时需要注册 MessageBus。使 Sender 连接到 Receiver 的最小 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);
  

但是,我还不知道为什么需要这样做。

【讨论】:

以上是关于Google Cast Android Sender:未调用 ResultCallback的主要内容,如果未能解决你的问题,请参考以下文章

Android:让 Google Cast MediaRouter 图标显示

设置 Google Cast 示例“CastHelloText-Android”时出错

在 Google Cast SDK 开发者控制台中注册 Android TV

将 Google Cast 集成到 Android TV 应用中

可以同时使用 Holo 主题和 google cast 的 Android 应用吗?

NoClassDefFoundError java.lang.NoClassDefFoundError com.google.sample.castcompanionlibrary.cast.Vide