Courtesy: GitHub

Android PictureInPicture Sample

This sample demonstrates basic usage of Picture-in-Picture mode for handheld devices. The sample plays a video. The video keeps on playing when the app is turned in to Picture-in-Picture mode. On Picture-in-Picture screen, the app shows an action item to pause or resume the video.

Introduction

As of Android O, activities can launch in Picture-in-Picture (PiP) mode. PiP is a special type of multi-window mode mostly used for video playback.

The app is paused when it enters PiP mode, but it should continue showing content. For this reason, you should make sure your app does not pause playback in its onPause() handler. Instead, you should pause video in onStop(). For more information, see Multi-Window Lifecycle.

To specify that your activity can use PIP mode, set android:supportsPictureInPicture to true in the manifest. (Beginning with Android O, you do not need to set android:resizeableActivity to true if you are supporting PIP mode you only need to setrandroid:resizeableActivity if your activity supports other multi-window modes.)

You can pass a PictureInPictureParams to enterPictureInPictureMode() to specify how an activity should behave when it is in PiP mode. You can also use it to call setPictureInPictureParams() and update the current behavior. If the app is in not PiP mode, it will be used for later call of enterPictureInPictureMode().

With a PictureInPictureParams, you can specify aspect ratio of PiP activity and action items available for PiP mode. The aspect ratio is used when the activity is in PiP mode. The action items are used as menu items in PiP mode. You can use a PendingIntent to specify what to do when the item is selected.

Pre-requisites

  • Android SDK 26
  • Android Build Tools v26.0.1
  • Android Support Repository

Screenshots

Screenshot Screenshot

Getting Started

This sample uses the Gradle build system. To build this project, use the "gradlew build" command or use "Import Project" in Android Studio.

Support

If you've found an error in this sample, please file an issue: https://github.com/googlesamples/android-PictureInPicture

Patches are encouraged, and may be submitted by forking this project and submitting a pull request through GitHub. Please see CONTRIBUTING.md for more details.

License

Copyright 2017 The Android Open Source Project, Inc.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

compileSdkVersion 26

applicationId "com.example.android.pictureinpicture"

minSdkVersion 26

targetSdkVersion 26

versionCode 1

versionName "1.0"

compile fileTree(dir: 'libs', include: ['*.jar'])

compile 'com.android.support:appcompat-v7:26.1.0'

compile 'com.android.support:support-media-compat:26.1.0'

testCompile 'junit:junit:4.12'

compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

compileSdkVersion 26

applicationId "com.example.android.pictureinpicture"

minSdkVersion 26

targetSdkVersion 26

versionCode 1

versionName "1.0"

compile fileTree(dir: 'libs', include: ['*.jar'])

compile 'com.android.support:appcompat-v7:26.1.0'

compile 'com.android.support:support-media-compat:26.1.0'

testCompile 'junit:junit:4.12'

package com.example.android.pictureinpicture

package com.example.android.pictureinpicture

MainActivity

MediaSessionPlaybackActivity

MainActivity

Demonstrates usage of Picture-in-Picture mode on phones and tablets.

Intent action for media controls from Picture-in-Picture mode.

Intent extra for media controls from Picture-in-Picture mode.

The request code for play action PendingIntent.

The request code for pause action PendingIntent.

The request code for info action PendingIntent.

The intent extra value for play action.

The intent extra value for pause action.

The arguments to be used for Picture-in-Picture mode.

This shows the video.

The bottom half of the screen; hidden on landscape

A {@link BroadcastReceiver} to receive action item events from Picture-in-Picture mode.

Callbacks from the {@link MovieView} showing the video playback.

Update the state of pause/resume action item in Picture-in-Picture mode. * @param iconId The icon to be used. @param title The title text. @param controlType The type of the action. either {@link #CONTROL_TYPE_PLAY} or {@link

CONTROL_TYPE_PAUSE}.

@param requestCode The request code for the {@link PendingIntent}.

Enters Picture-in-Picture mode.

Adjusts immersive full-screen flags depending on the screen orientation. * @param config The current {@link Configuration}.

Launches {@link MediaSessionPlaybackActivity} and closes this activity.

MediaSessionPlaybackActivity

Demonstrates usage of Picture-in-Picture when using {@link android.support.v4.media.session.MediaSessionCompat}.

The arguments to be used for Picture-in-Picture mode.

This shows the video.

The bottom half of the screen; hidden on landscape

Callbacks from the {@link MovieView} showing the video playback.

Enters Picture-in-Picture mode.

Adjusts immersive full-screen flags depending on the screen orientation. * @param config The current {@link Configuration}.

Overloaded method that persists previously set media actions. * @param state The state of the video, e.g. playing, paused, etc. @param position The position of playback in the video. @param mediaId The media id related to the video in the media session.

Updates the {@link MovieView} based on the callback actions.
Simulates a playlist that will disable actions when you cannot skip through the playlist in a certain direction.