我正面临一个问题,试图用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但我不知道是什么导致了这个问题。任何帮助都将不胜感激,谢谢。
发布于 2014-10-02 09:06:08
我解决了我的问题。我只需在设置incrementUiCounter时在onResume中添加一个对videoCastConsumer的调用。
@Override
public void onResume() {
super.onResume();
// CHROMECAST
mVideoCastManager = MyApplication.getVideoCastManager(this);
mVideoCastManager.addVideoCastConsumer(mVideoCastConsumer);
mVideoCastManager.incrementUiCounter();
...https://stackoverflow.com/questions/26119909
复制相似问题