https://developer.android.com/guide/topics/ui/picture-in-picture?hl=ko
https://developer.android.com/about/versions/oreo/android-8.0?hl=ko#opip
1. 기본구성
- AndroidManifest.xml
<activity
android:name=".MainActivity"
android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
android:resizeableActivity="true"
android:supportsPictureInPicture="true">
- 미디어 플레이어 코드가 구현된 상태에서 Actvity 추가사항들.
PictureInPictureParams.Builder pictureInPictureParamsBuilder = new PictureInPictureParams.Builder();
Rational aspectRatio = new Rational(mediaPlayer.getVideoWidth(), mediaPlayer.getVideoHeight());
PictureInPictureParams build = pictureInPictureParamsBuilder.setAspectRatio(aspectRatio).build();
enterPictureInPictureMode(build);
@Override
public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
super.onPictureInPictureModeChanged(isInPictureInPictureMode);
Log.d(TAG, "onPictureInPictureModeChanged : " + isInPictureInPictureMode);
}
> PIP 동작 상태가 변경되면 onPictureInPictureModeChanged 메소드가 호출됩니다.
> enterPictureInPictureMode 메소드를 호출하면 PIP 모드로 동작합니다.
- Activity 전체코드
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private String mediaURL = "https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_20mb.mp4";
private MediaPlayer mediaPlayer;
private boolean minimize;
private final Object pictureInPictureParamsBuilder;
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
pictureInPictureParamsBuilder = new PictureInPictureParams.Builder();
} else {
pictureInPictureParamsBuilder = null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
SurfaceView surfaceView = findViewById(R.id.surfaceView);
surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
initMediaPlayer(holder);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
});
View actionPIP = findViewById(R.id.actionPIP);
actionPIP.setVisibility(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? View.VISIBLE : View.GONE);
actionPIP.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
minimize();
}
});
}
private void initMediaPlayer(SurfaceHolder holder) {
holder.getSurface();
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mediaPlayer.setSurface(holder.getSurface());
try {
mediaPlayer.setDataSource(getBaseContext(), Uri.parse(mediaURL));
mediaPlayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}
private void play() {
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying() == false) {
mediaPlayer.start();
}
}
}
@Override
protected void onResume() {
super.onResume();
play();
}
@Override
protected void onPause() {
super.onPause();
if (minimize == false)
if (mediaPlayer != null) {
mediaPlayer.pause();
}
}
@Override
protected void onDestroy() {
if (mediaPlayer != null) {
mediaPlayer.release();
}
super.onDestroy();
}
/**
* PIP 모드
*/
private void minimize() {
if (mediaPlayer == null) {
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (pictureInPictureParamsBuilder instanceof PictureInPictureParams.Builder) {
Rational aspectRatio = new Rational(mediaPlayer.getVideoWidth(), mediaPlayer.getVideoHeight());
PictureInPictureParams build = ((PictureInPictureParams.Builder) pictureInPictureParamsBuilder).setAspectRatio(aspectRatio).build();
minimize = true;
enterPictureInPictureMode(build);
}
}
}
@Override
public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
super.onPictureInPictureModeChanged(isInPictureInPictureMode);
Log.d(TAG, "onPictureInPictureModeChanged : " + isInPictureInPictureMode);
if (isInPictureInPictureMode) {
play();
} else {
minimize = false;
}
}
}
3. 샘플코드
소스코드
APK
샘플 mp4 영상
https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_20mb.mp4
구글 샘플코드
https://github.com/googlesamples/android-PictureInPicture/#readme