我的Android应用使用远程显示API将视频投射到用户的支持Cast的设备。不幸的是,我们必须使用专有的视频播放器,因此我无法使用普通的视频API。遗憾的是,这是我无法控制的。
该应用等待用户选择显示并启动视频。我会说它在大约50%的时间里运行良好。通常,在视频继续播放时,音频将停止播放。有时(但更少见)相反的情况发生 - 当音频继续时,Cast屏幕变黑。视频播放时经常会有音频和视频跳过,只有在设备上观看时才会有体验。
我通过远程显示器收集播放视频并不理想,但我认为音频和视频应该继续流式传输,特别是因为Remote Display是为图形密集型游戏而设计的。
此外,当活动被推入后台时音频停止的事实是一个破坏性的事情。有没有计划改变这个?
以下是一些代码,展示了我如何创建连接和启动视频。也许我做一些愚蠢的事情导致它表现不佳?
当用户从MediaRouteChooserDialog中选择设备时,将调用此代码:
@Override
public void onRouteSelected(MediaRouter router, MediaRouter.RouteInfo info) {
selectedDevice = CastDevice.getFromBundle(info.getExtras());
Intent intent = new Intent(mainActivity,
ExampleMainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("cast", true);
PendingIntent notificationPendingIntent = PendingIntent.getActivity(mainActivity, 0, intent, 0);
CastRemoteDisplayLocalService.NotificationSettings settings =
new CastRemoteDisplayLocalService.NotificationSettings.Builder()
.setNotificationPendingIntent(notificationPendingIntent).build();
CastRemoteDisplayLocalService.startService(
mainActivity,
ExamplePresentationService.class,
config.getCastId(castButton.getContext()),
selectedDevice,
settings,
new CastRemoteDisplayLocalService.Callbacks() {
@Override
public void onRemoteDisplaySessionStarted(CastRemoteDisplayLocalService service) {
Log.d(TAG, "onServiceStarted");
}
@Override
public void onRemoteDisplaySessionError(Status errorReason) {
Log.d(TAG, "onServiceError: " + errorReason.getStatusCode());
}
}
);
}
我的CastRemoteDisplayLocalService在其createPresentation方法中创建CastPresentation:
@TargetApi(17)
private void createPresentation(Display display) {
dismissPresentation();
mPresentation = new PresentationPlayer(this, display, castHelper, adManager);
try {
mPresentation.show();
//mMediaPlayer.start();
} catch (WindowManager.InvalidDisplayException ex) {
Log.e(TAG, "Unable to show presentation, display was removed.", ex);
dismissPresentation();
}
}
当用户选择视频时,在CastPresentation中执行以下代码:
public void startVideo(VideoData data) {
FrameLayout videoBase = (FrameLayout)findViewById(R.id.cast_video_frame);
videoBase.setVisibility(View.VISIBLE);
toggleLogoScreen(false);
if (player != null) {
player.stop();
player.close();
player = null;
videoBase.setVisibility(View.VISIBLE);
}
player = CvpPlayer.create(PlayerConstants.PlayerType.NEXSTREAM, castHelper.getExampleMainActivity(), videoBase);
player.setPlayerListener(this);
player.initPlayer();
}
非常感谢任何想法。