isSupportPipMode = getPackageManager().hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N;
if(videoPlayer!= null) {
videoPlayer.setSupportPipMode(isSupportPipMode);
}
在您的 Activity 中,替换 onNewIntent() 并处理新的视频,从而根据需要停止任何现有的视频播放。
/**
* 进入画中画模式
*/
private PictureInPictureParams.Builder mPictureInPictureParamsBuilder;
private void enterPiPMode() {
if (videoPlayer == null) {
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
videoPlayer.setIsInPictureInPictureMode(true);
if (mPictureInPictureParamsBuilder == null) {
mPictureInPictureParamsBuilder = new PictureInPictureParams.Builder();
}
// Calculate the aspect ratio of the PiP screen. 计算video的纵横比
mVideoWith = videoPlayer.getCurrentVideoWidth();
mVideoHeight = videoPlayer.getCurrentVideoHeight();
if (mVideoWith != 0 && mVideoHeight != 0) {
//设置param宽高比,根据宽高比例调整初始参数
Rational aspectRatio = new Rational(mVideoWith, mVideoHeight);
mPictureInPictureParamsBuilder.setAspectRatio(aspectRatio);
}
//进入pip模式
enterPictureInPictureMode(mPictureInPictureParamsBuilder.build());
}
}
@Override
public void onPictureInPictureModeChanged (boolean isInPictureInPictureMode, Configuration newConfig) {
if (isInPictureInPictureMode) {
// Hide the full-screen UI (controls, etc.) while in picture-in-picture mode.
} else {
// Restore the full-screen UI.
...
}
}
@Override
public void onPause() {
// If called while in PIP mode, do not pause playback
if (isInPictureInPictureMode()) {
// Continue playback
...
} else {
// Use existing playback logic for paused Activity behavior.
...
}
}
/**
* 视频尺寸变化(上一个下一个时),动态调整PIP 宽高比
*
* @param with video宽度(非界面宽度)
* @param height video高度(非界面高度)
*/
private void videoSizeChange(int with, int height) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && (height != mVideoHeight || mVideoWith != with)) {
mVideoWith = with;
mVideoHeight = height;
if (mPictureInPictureParamsBuilder != null && mVideoWith != 0 && mVideoHeight != 0) {
//设置param宽高比,根据快高比例调整初始参数
Rational aspectRatio = new Rational(mVideoWith, mVideoHeight);
mPictureInPictureParamsBuilder.setAspectRatio(aspectRatio);
//设置更新PictureInPictureParams
setPictureInPictureParams(mPictureInPictureParamsBuilder.build());
}
}
}
private MediaSessionCompat mSession;
public static final long MEDIA_ACTIONS_PLAY_PAUSE = PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_PLAY_PAUSE;
public static final long MEDIA_ACTIONS_ALL = MEDIA_ACTIONS_PLAY_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS;
private void initializeMediaSession() {
mSession = new MediaSessionCompat(this, TAG);
mSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mSession.setActive(true);
MediaControllerCompat.setMediaController(this, mSession.getController());
MediaMetadataCompat metadata = new MediaMetadataCompat.Builder().build();
mSession.setMetadata(metadata);
MediaSessionCallback mMediaSessionCallback = new MediaSessionCallback(videoPlayer);
mSession.setCallback(mMediaSessionCallback);
int state = videoPlayer.getCurrentState() == GSYVideoView.CURRENT_STATE_PLAYING ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED;
updatePlaybackState(state, MEDIA_ACTIONS_ALL, 0, 0);
}
在MediaSessionCompat.Callback中设置自己的播放器逻辑响应
private class MediaSessionCallback extends MediaSessionCompat.Callback {
private LocalListVideoPlayer movieView;
private int indexInPlaylist;
public MediaSessionCallback(LocalListVideoPlayer movieView) {
this.movieView = movieView;
indexInPlaylist = 1;
}
@Override
public void onPlay() {
super.onPlay();
movieView.getGSYVideoManager().start();
movieView.setIsInPictureInPictureMode(true);
movieView.setCurrentState(GSYVideoView.CURRENT_STATE_PLAYING);
updatePlaybackState(PlaybackStateCompat.STATE_PLAYING, 0, 0);
}
@Override
public void onPause() {
super.onPause();
movieView.getGSYVideoManager().pause();
movieView.setIsInPictureInPictureMode(true);
movieView.setCurrentState(GSYVideoView.CURRENT_STATE_PAUSE);
updatePlaybackState(PlaybackStateCompat.STATE_PAUSED, 0, 0);
}
@Override
public void onSkipToNext() {
super.onSkipToNext();
movieView.playNext();
}
@Override
public void onSkipToPrevious() {
super.onSkipToPrevious();
movieView.playLast();
}
}
//更新按钮操作
private void updatePlaybackState(@PlaybackStateCompat.State int state, int position, int mediaId) {
if (mSession.getController().getPlaybackState() != null) {
long actions = mSession.getController().getPlaybackState().getActions();
updatePlaybackState(state, actions, position, mediaId);
}
}
//初始化setPlaybackState
private void updatePlaybackState(@PlaybackStateCompat.State int state, long playbackActions, int position, int mediaId) {
if (mSession != null) {
PlaybackStateCompat.Builder builder = new PlaybackStateCompat.Builder()
.setActions(playbackActions)
.setActiveQueueItemId(mediaId)
.setState(state, position, 1.0f);
mSession.setPlaybackState(builder.build());
}
}
在自己播放器状态更新时更新界面元素
@Override
public void onVideoStart() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
if(isInPictureInPictureMode()) {
updatePlaybackState(PlaybackStateCompat.STATE_PLAYING, 0, 0);
}
}
}
@Override
public void onVideoPause() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
if(isInPictureInPictureMode()) {
updatePlaybackState(PlaybackStateCompat.STATE_PAUSED, 0, 0);
}
}
}
首先自定义按钮初始化或刷新
private BroadcastReceiver mReceiver;
private static final String ACTION_MEDIA_CONTROL = "media_control";
private static final String EXTRA_CONTROL_TYPE = "control_type";
private static final int CONTROL_TYPE_PLAY = 1;
private static final int CONTROL_TYPE_PAUSE = 2;
private static final int CONTROL_TYPE_LAST = 3;
private static final int CONTROL_TYPE_NEXT = 4;
private static final int REQUEST_TYPE_PLAY = 1;
private static final int REQUEST_TYPE_PAUSE = 2;
private static final int REQUEST_TYPE_LAST = 3;
private static final int REQUEST_TYPE_NEXT = 4;
//进入画中画前判断状态,调用initPictureInPictureActions
private void initPictureInPictureActions() {
//int state = videoPlayer.getCurrentState() == GSYVideoView.CURRENT_STATE_PLAYING ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED;
//STATE_PLAYING = 3 ; STATE_PAUSED = 2
if (videoPlayer.getCurrentState() == GSYVideoView.CURRENT_STATE_PLAYING) {
updatePictureInPictureActions(R.drawable.gsy_play_video_icon_pause, "", CONTROL_TYPE_PLAY, REQUEST_TYPE_PLAY);
} else {
updatePictureInPictureActions(R.drawable.gsy_play_video_icon_play, "", CONTROL_TYPE_PAUSE, REQUEST_TYPE_PAUSE);
}
}
/**
* 刷新自定义按钮 (若是初始化,注意区分进入画中画前onpause状态)
*
* @param iconId
* @param title
* @param controlType
* @param requestCode 注意!! 每个intent的requestCode必须不一样
*/
void updatePictureInPictureActions(@DrawableRes int iconId, String title, int controlType, int requestCode) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (mPictureInPictureParamsBuilder == null) {
mPictureInPictureParamsBuilder = new PictureInPictureParams.Builder();
}
final ArrayList actions = new ArrayList<>();
// This is the PendingIntent that is invoked when a user clicks on the action item. You need to use distinct request codes for play and pause, or the PendingIntent won't be updated.
//上一个
final PendingIntent intentLast = PendingIntent.getBroadcast(this, REQUEST_TYPE_NEXT, new Intent(ACTION_MEDIA_CONTROL).putExtra(EXTRA_CONTROL_TYPE, CONTROL_TYPE_LAST), 0);
actions.add(new RemoteAction(Icon.createWithResource(this, R.drawable.gsy_play_video_icon_last), "", "", intentLast));
//暂停/播放
final PendingIntent intentPause = PendingIntent.getBroadcast(this, requestCode, new Intent(ACTION_MEDIA_CONTROL).putExtra(EXTRA_CONTROL_TYPE, controlType), 0);
actions.add(new RemoteAction(Icon.createWithResource(this, iconId), title, title, intentPause));
//下一个
final PendingIntent intentNext = PendingIntent.getBroadcast(this, REQUEST_TYPE_LAST, new Intent(ACTION_MEDIA_CONTROL).putExtra(EXTRA_CONTROL_TYPE, CONTROL_TYPE_NEXT), 0);
actions.add(new RemoteAction(Icon.createWithResource(this, R.drawable.gsy_play_video_icon_next), "", "", intentNext));
mPictureInPictureParamsBuilder.setActions(actions);
// This is how you can update action items (or aspect ratio) for Picture-in-Picture mode. Note this call can happen even when the app is not in PiP mode.
setPictureInPictureParams(mPictureInPictureParamsBuilder.build());
}
}
@Override
public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration newConfig) {
super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
if (videoPlayer != null) {
isInPIPMode = isInPictureInPictureMode;
videoPlayer.setIsInPictureInPictureMode(isInPIPMode);
}
//自定义action形式
if (isInPictureInPictureMode) {
// Starts receiving events from action items in PiP mode.
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent == null || !ACTION_MEDIA_CONTROL.equals(intent.getAction())) {
return;
}
// This is where we are called back from Picture-in-Picture action
final int controlType = intent.getIntExtra(EXTRA_CONTROL_TYPE, 0);
try {
switch (controlType) {
case CONTROL_TYPE_PLAY:
videoPlayer.getGSYVideoManager().start();
videoPlayer.setIsInPictureInPictureMode(true);
videoPlayer.setCurrentState(GSYVideoView.CURRENT_STATE_PLAYING);
break;
case CONTROL_TYPE_PAUSE:
videoPlayer.getGSYVideoManager().pause();
videoPlayer.setIsInPictureInPictureMode(true);
videoPlayer.setCurrentState(GSYVideoView.CURRENT_STATE_PAUSE);
break;
case CONTROL_TYPE_LAST:
videoPlayer.playLast();
break;
case CONTROL_TYPE_NEXT:
videoPlayer.playNext();
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
registerReceiver(mReceiver, new IntentFilter(ACTION_MEDIA_CONTROL));
} else {
// We are out of PiP mode. We can stop receiving events from it.
unregisterReceiver(mReceiver);
mReceiver = null;
}
}
当播放状态改变时更新按钮功能
videoPlayer.setLocalPlayerCallback(new LocalListVideoPlayer.LocalPlayerCallback() {
@Override
public void clickPIPMode() {
enterPiPMode();
}
@Override
public void OnPrepareVideoSizeChanged(int with, int height) {
videoSizeChange(with, height);
}
@Override
public void surfaceDestroyed() {
handleSurfaceDestroyed();
}
@Override
public void onVideoStart() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//自定义action刷新-开始播放-按钮替换为暂停
updatePictureInPictureActions(R.drawable.gsy_play_video_icon_pause, "", CONTROL_TYPE_PLAY, REQUEST_TYPE_PLAY);
}
}
@Override
public void onVideoPause() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//自定义action刷新-暂停播放,按钮替换为开始
updatePictureInPictureActions(R.drawable.gsy_play_video_icon_play, "", CONTROL_TYPE_PAUSE, REQUEST_TYPE_PAUSE);
}
private SurfaceView emptySurfaceView;
....
emptySurfaceView = findViewById(R.id.emtpy_surface);
emptySurfaceView
.getHolder()
.addCallback(
new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if(mLocalPlayerCallback != null) {
mLocalPlayerCallback.surfaceDestroyed();
}
}
});
操作画中画时VideoActivity相关生命周期梳理:
进入画中画--onPause
画中画返回全屏--OnResume
关闭画中画--onStop
全屏播放状态下下锁屏/解锁 onPause ,onStop / onStart,onResume
画中画状态下下锁屏/解锁 onStop / onStart
//是否支持pip画中画小窗模式(自行判断赋值时机)
protected boolean isSupportPipMode = false;
//是否已经在画中画模式(自行判断赋值时机)
public boolean isInPIPMode = false;
//是否点击进入过画中画模式--用于判断程序在后台时,由画中画返回全屏后退出,是否启动首页activity,以及onstop配合判断是否点击进入过画中画且在画中画模式
public boolean isEnteredPIPMode = false;
@Override
protected void onResume() {
super.onResume();
//画中画返回全屏会执行onresume
isEnteredPIPMode = false;
}
@Override
protected void onStop() {
super.onStop();
//备注: 在画中画模式下,onStop执行时, 若是关闭画中画,isInPictureInPictureMode()=false ; 若是锁屏,isInPictureInPictureMode()=true ; 判断锁屏isLockPage()一直为false
boolean inPictureInPictureMode = false;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
inPictureInPictureMode = isInPictureInPictureMode();
}
if (BuildConfig.DEBUG) {
Log.i(TAG, "onStop -- inPictureInPictureMode=" + inPictureInPictureMode + " ,isEnteredPIPMode=" + isEnteredPIPMode + " ,isInPIPMode=" + isInPIPMode);
}
if (!inPictureInPictureMode && isInPIPMode && isEnteredPIPMode) {
//满足此条件下认为是关闭了画中画界面
if (BuildConfig.DEBUG) {
Log.w(TAG, "onStop -- 判断为PIP下关闭画中画");
}
handleSurfaceDestroyed();
return;
}
if (inPictureInPictureMode && isInPIPMode && isEnteredPIPMode && videoPlayer != null) {
//满足此条件下认为是画中画模式下锁屏
videoPlayer.onVideoPause();
isPause = true;
if (BuildConfig.DEBUG) {
Log.w(TAG, "onStop -- 判断为PIP下锁屏");
}
}
}
manifest添加 android:excludeFromRecents="true"
参考:关于Android TaskAffinity的那些事儿
From Picture-in-Picture activity to Back-Stack activity not working in android?
主页设置 android:supportsPictureInPicture="false"无效
方案:采用遍历tasks,task.moveToFront() - task.moveToFront(); 避免采用startActivity方法使应用回到前台
参考:Launching Intent from notification opening in picture-in-picture window
public static void moveLauncherTaskToFront(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
assert activityManager != null;
final List appTasks = activityManager.getAppTasks();
for (ActivityManager.AppTask task : appTasks) {
final Intent baseIntent = task.getTaskInfo().baseIntent;
final Set categories = baseIntent.getCategories();
if (categories != null && categories.contains(Intent.CATEGORY_LAUNCHER)) {
task.moveToFront();
return;
}
}
}
判断获取用户是否关闭了应用画中画模式
当您的应用处于画中画模式时,画中画窗口中的视频播放可能会对其他应用(例如,音乐播放器应用或语音搜索应用)造成音频干扰。为避免出现此问题,请在开始播放视频时请求音频焦点,并处理音频焦点更改通知,如管理音频焦点中所述。如果您在处于画中画模式时收到音频焦点丢失通知,请暂停或停止视频播放。
//音频焦点的监听
protected AudioManager mAudioManager;
mAudioManager = (AudioManager) getActivityContext().getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
/**
* 监听是否有外部其他多媒体开始播放
*/
protected AudioManager.OnAudioFocusChangeListener onAudioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN:
//获得了Audio Focus
onGankAudio();
break;
case AudioManager.AUDIOFOCUS_LOSS:
//失去了Audio Focus,并将会持续很长的时间-暂停音频
onLossAudio();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
//暂时失去Audio Focus,并会很快再次获得
onLossTransientAudio();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
//暂时失去AudioFocus,但是可以继续播放,不过要在降低音量
onLossTransientCanDuck();
break;
}
}
};
app: MXplayer: (顶层activity: pipmenuActivity)
创造 “魔术时刻” —— Android 8.0 画中画
Google文档画中画 / 多窗口支持 / pipmenuActivity
Android 8.0 PictureInPicture 画中画模式分析与使用
Android开发笔记(一百六十七)Android8.0的画中画模式
画中画权限判断 / 用户关闭画中画模式
画中画github示例
Android 8.0 Oreo 画中画模式
关于Android TaskAffinity的那些事儿
From Picture-in-Picture activity to Back-Stack activity not working in android?
Launching Intent from notification opening in picture-in-picture window
How Do We Leave Picture-In-Picture Mode?
Android Picture in picture mode and backstack management for multiple activities