CastCompanionLibrary : mApiClient 或 mRemoteMediaPlayer 为空

Posted

技术标签:

【中文标题】CastCompanionLibrary : mApiClient 或 mRemoteMediaPlayer 为空【英文标题】:CastCompanionLibrary : mApiClient or mRemoteMediaPlayer is null 【发布时间】:2014-09-30 11:37:23 【问题描述】:

我在尝试使用 CastCompanionLibrary 在我的应用程序上实现 Chromecast 时遇到问题。

这是我初始化 CastManager 的方法。

public static VideoCastManager mCastMgr;
//chromeCast init
public static VideoCastManager getVideoCastManager(Context ctx) 

    if (mCastMgr == null) 
        mCastMgr = VideoCastManager.initialize(ctx, ctx.getResources().getString(R.string.CAST_APPLICATION_ID), null, null); 
        mCastMgr.enableFeatures(VideoCastManager.FEATURE_NOTIFICATION | VideoCastManager.FEATURE_LOCKSCREEN |
                VideoCastManager.FEATURE_WIFI_RECONNECT 
                | VideoCastManager.FEATURE_NOTIFICATION |
                VideoCastManager.FEATURE_DEBUGGING);
    
    mCastMgr.setContext(ctx);
    return mCastMgr;

这里只是显示一种媒体的测试代码。该方法在 onCreate 中被调用。

private     MediaRouteButton    mMediaRouteButton;  
private     VideoCastManager    mVideoCastManager;
private     VideoCastConsumerImpl mVideoCastConsumer;

private void setupChromeCast() 
    mVideoCastManager = MyApplication.getVideoCastManager(this);
    mMediaRouteButton = (MediaRouteButton) findViewById(R.id.media_route_button);
    mVideoCastManager.addMediaRouterButton(mMediaRouteButton);

    mMediaRouteButton.setVisibility(View.VISIBLE);

    mVideoCastConsumer = new VideoCastConsumerImpl() 
        //          .....
        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public void onCastAvailabilityChanged(boolean castPresent) 
            super.onCastAvailabilityChanged(castPresent);
            mMediaRouteButton.setVisibility(castPresent ? View.VISIBLE : View.INVISIBLE);
        

        @Override
        public void onApplicationConnected(ApplicationMetadata appMetadata,
                String sessionId, boolean wasLaunched) 
            Bundle mediaInfo = new Bundle();
            mediaInfo.putString(MediaMetadata.KEY_TITLE, "toto");
            mediaInfo.putString(MediaMetadata.KEY_SUBTITLE, "le roro");
            mediaInfo.putString(MediaMetadata.KEY_STUDIO, "mdr");
            mediaInfo.putString("movie-urls", "https://ia902302.us.archive.org/27/items/Pbtestfilemp4videotestmp4/video_test.mp4");
            mediaInfo.putString("content-type", "video/mp4");
            ArrayList<String> images = new ArrayList<String>();
            images.add("https://scontent-a-cdg.xx.fbcdn.net/hphotos-xpa1/v/t1.0-9/10426650_10203984861039843_6191993092115493723_n.jpg?oh=8cb095f97c4b8f7d726d4a4d418d65fc&oe=54C484F9");
            images.add("http://upload.wikimedia.org/wikipedia/commons/f/f3/Toto'025.jpg");
            mediaInfo.putStringArrayList("images", images);
            MediaInfo mSelectedMedia = com.google.sample.castcompanionlibrary.utils.Utils.toMediaInfo(mediaInfo);
            try 
                mVideoCastManager.startCastControllerActivity(getApplicationContext(), mSelectedMedia, 0, true);
                finish();
                return ;
             catch (IllegalStateException e) 
              e.printStackTrace();  
             catch (Exception e) 
              e.printStackTrace();
            
        

    ;

然后在 onResume 中,我将我的 VideoCastConsumer 分配给我的经理。

// CHROMECAST
    mVideoCastManager.addVideoCastConsumer(mVideoCastConsumer);

我对 Chromecast 检测没有任何问题,它运行良好。我用应用程序选择我的设备,然后它在我的屏幕上显示我的应用程序名称,什么也不做。原因必须在此调试消息中。

09-30 17:36:26.164: D/ccl_CastMediaRouterCal(7630): [v1.10] Route added: Chambre
09-30 17:36:34.997: D/ccl_BaseCastManager(7630): [v1.10] Successfully added the new BaseCastConsumer listener com.MYAPP.androidApp.PlayerActivity$12@420459e0
09-30 17:36:34.997: D/ccl_VideoCastManager(7630): [v1.10] Successfully added the new CastConsumer listener com.MYAPP.AndroidApp.PlayerActivity$12@420459e0
09-30 17:36:38.576: D/ccl_CastMediaRouterCal(7630): [v1.10] onRouteSelected: info=MediaRouter.RouteInfo uniqueId=com.google.android.gms/.cast.media.CastMediaRouteProviderService:92ec2ab63d7e51ff96603bf08487fbc6, name=Chambre, description=Chromecast, enabled=true, connecting=false, playbackType=1, playbackStream=-1, volumeHandling=0, volume=0, volumeMax=20, presentationDisplayId=-1, extras=Bundle[mParcelledData.dataSize=580], providerPackageName=com.google.android.gms 
09-30 17:36:38.578: D/ccl_BaseCastManager(7630): [v1.10] acquiring a connection to Google Play services for "Chambre" (92ec2ab63d7e51ff96603bf08487fbc6)
09-30 17:36:38.590: D/ccl_CastMediaRouterCal(7630): [v1.10] onResult: mSelectedDevice=Chambre
09-30 17:36:39.593: D/ccl_BaseCastManager(7630): [v1.10] onConnected() reached with prior suspension: false
09-30 17:36:39.598: D/ccl_BaseCastManager(7630): [v1.10] launchApp() is called
09-30 17:36:39.598: D/ccl_BaseCastManager(7630): [v1.10] Launching app
09-30 17:36:39.640: D/ccl_VideoCastManager(7630): [v1.10] onVolumeChanged() reached
09-30 17:36:39.641: D/ccl_VideoCastManager(7630): [v1.10] onApplicationStatusChanged() reached: 
09-30 17:36:39.757: D/ccl_VideoCastManager(7630): [v1.10] onApplicationStatusChanged() reached: null
09-30 17:36:40.988: D/ccl_BaseCastManager(7630): [v1.10] launchApplication() -> success result
09-30 17:36:40.988: D/ccl_VideoCastManager(7630): [v1.10] onApplicationConnected() reached with sessionId: A5E3356F-14BD-FD02-6044-8911E8E4D215, and mReconnectionStatus=INACTIVE
09-30 17:36:40.989: D/ccl_VideoCastManager(7630): [v1.10] startNotificationService()
09-30 17:36:40.994: D/ccl_VideoCastManager(7630): [v1.10] attachMedia()
09-30 17:36:40.996: D/ccl_VideoCastManager(7630): [v1.10] Registering MediaChannel namespace
09-30 17:36:41.007: D/ccl_VideoCastManager(7630): [v1.10] onApplicationStatusChanged() reached: Ready To Cast
09-30 17:36:41.008: D/ccl_VideoCastNotificat(7630): [v1.10] onCreate()
09-30 17:36:41.009: D/ccl_BaseCastManager(7630): [v1.10] Successfully added the new BaseCastConsumer listener com.google.sample.castcompanionlibrary.notification.VideoCastNotificationService$1@41f7edb0
09-30 17:36:41.009: D/ccl_VideoCastManager(7630): [v1.10] Successfully added the new CastConsumer listener com.google.sample.castcompanionlibrary.notification.VideoCastNotificationService$1@41f7edb0
09-30 17:36:41.009: D/ccl_VideoCastNotificat(7630): [v1.10] onStartCommand
09-30 17:36:41.010: D/ccl_VideoCastNotificat(7630): [v1.10] onStartCommand(): Action: ACTION_VISIBILITY true
09-30 17:36:41.684: D/ccl_VideoCastManager(7630): [v1.10] RemoteMediaPlayer::onStatusUpdated() is reached
09-30 17:36:41.684: D/ccl_VideoCastManager(7630): [v1.10] onRemoteMediaPlayerStatusUpdated() reached
09-30 17:36:41.684: D/ccl_VideoCastManager(7630): [v1.10] mApiClient or mRemoteMediaPlayer is null, so will not proceed
09-30 17:36:41.684: D/ccl_VideoCastManager(7630): [v1.10] RemoteMediaPlayer::onMetadataUpdated() is reached
09-30 17:36:41.684: D/ccl_VideoCastManager(7630): [v1.10] onRemoteMediaPlayerMetadataUpdated() reached

这是 Chromecast 日志,但在我断开连接之前似乎什么都没有。这很奇怪。

Failed to load resource: the server responded with a status of 404 (Not Found) https://www.gstatic.com/eureka/player/undefined
 [  0.280s] [goog.net.WebSocket] Opening the WebSocket on ws://localhost:8008/v2/ipc cast_receiver.js:18
The page at 'https://www.gstatic.com/eureka/player/player.html?skin' was loaded over HTTPS, but ran insecure content from 'ws://localhost:8008/v2/ipc': this content should also be loaded over HTTPS.
 player.html:1
Connecting to a non-secure WebSocket server from a secure origin is deprecated. player.html:1
 [  0.363s] [goog.net.WebSocket] WebSocket opened on ws://localhost:8008/v2/ipc cast_receiver.js:18
Failed to load resource: the server responded with a status of 404 (Not Found) https://www.gstatic.com/eureka/player/undefined
cast.receiver.logger.setLevelValue(cast.receiver.LoggerLevel.DEBUG);
undefined
 [ 17.665s] [cast.receiver.IpcChannel] Received message: "data":"\"reason\":\"closed_by_peer\",\"senderId\":\"35:com.MY_APP.AndroidApp-12\",\"type\":\"senderdisconnected\"","namespace":"urn:x-cast:com.google.cast.system","senderId":"SystemSender" cast_receiver.js:18
 [ 17.676s] [cast.receiver.CastMessageBus] Dispatching CastMessageBus message cast_receiver.js:18
 [ 17.682s] [cast.receiver.CastReceiverManager] Dispatching sender disconnected event [35:com.MY_APP.AndroidApp-12] Reason: requested_by_sender cast_receiver.js:18
 [ 17.688s] [cast.receiver.CastMessageBus] Unregistering sender [urn:x-cast:com.google.cast.player.message, 35:com.MY_APP.AndroidApp-12] cast_receiver.js:18
 [ 17.693s] [cast.receiver.CastMessageBus] Unregistering sender [urn:x-cast:com.google.cast.media, 35:com.MY_APP.AndroidApp-12] cast_receiver.js:18
 [ 17.704s] [cast.receiver.CastMessageBus] Disposed CastMessageBus[urn:x-cast:com.google.cast.system] cast_receiver.js:18
 [ 17.710s] [cast.receiver.CastMessageBus] Disposed CastMessageBus[urn:x-cast:com.google.cast.player.message] cast_receiver.js:18
 [ 17.716s] [cast.receiver.CastMessageBus] Disposed CastMessageBus[urn:x-cast:com.google.cast.media] cast_receiver.js:18
 [ 17.729s] [cast.receiver.CastReceiverManager] Disposed CastReceiverManager cast_receiver.js:18

但我不知道是什么导致了这个问题。任何帮助将不胜感激,谢谢。

【问题讨论】:

您需要提供 (1) 来自 android 端的更多日志和 (2) 来自您的投射设备的控制台日志。没有这些,很难看到正在发生的事情。此外,如果可以,请更新到 CCL 1.10。最后,是否调用了 onApplicationConnected? 我更新了 CCL 并且 onApplicationConnected 被称为是。我的投射设备中的控制台日志是什么意思? 您需要从您的接收器收集日志,请参阅此处的调试部分:developers.google.com/cast/docs/custom_receiver。另外,请添加更多来自 Android 端的日志;我很想看看在你展示的部分之后会发生什么;这还不足以解决问题 我正在尝试获取 Chromecast 日志。如果我理解您的链接,我可以在此处访问它:在 Google Chrome 上IP_ADRESS_OF_MY_CHROMECAST:9222。但我正在做它192.168.1.17:9222 这是它的地址,我有“这个网页不可用”。我的设备注册良好,我在正确的 wifi 上.. 你的 getVideoCastManager() 是在哪里定义的?是否在同一个活动中? 【参考方案1】:

我解决了我的问题。我只需要在设置 videoCastConsumer 时在 onResume 中添加对 incrementUiCounter 的调用。

@Override
public void onResume() 
    super.onResume();

    // CHROMECAST
    mVideoCastManager = MyApplication.getVideoCastManager(this);
    mVideoCastManager.addVideoCastConsumer(mVideoCastConsumer);
    mVideoCastManager.incrementUiCounter();
...

【讨论】:

以上是关于CastCompanionLibrary : mApiClient 或 mRemoteMediaPlayer 为空的主要内容,如果未能解决你的问题,请参考以下文章

CastCompanionLibrary 和不扩展 ActionBarActivity

使用 CastCompanionLibrary 将“播放列表”推送到 Chromecast

使用适用于 Android 的 CastCompanionLibrary 扫描设备

Google CastCompanionLibrary 许可证?

R 无法解析为 Eclipse 中 CastCompanionLibrary 上的变量

mediaroutebutton CastCompanionLibrary 上的连接动画