1page.title=Picture-in-picture 2page.keywords=preview,sdk,PIP,Picture-in-picture 3page.tags=androidn 4 5@jd:body 6 7<div id="qv-wrapper"> 8<div id="qv"> 9 10<h2>In this document</h2> 11<ol> 12 <li><a href="#declaring">Declaring Your Activity Supports 13Picture-in-picture</a></li> 14 <li><a href="#pip_button">Switching Your Activity to Picture-in-picture</a> 15</li> 16 <li><a href="#handling_ui">Handling UI During Picture-in-picture</a> 17</li> 18 <li><a href="#continuing_playback">Continuing Video Playback While in 19Picture-in-picture</a></li> 20 <li><a href="#single_playback">Using a Single Playback Activity for 21Picture-in-picture</a></li> 22 <li><a href="#best">Best Practices</a></li> 23</ol> 24 25<h2>See Also</h2> 26<ol> 27 <li><a href="{@docRoot}preview/features/multi-window.html">Multi-Window 28Support</a></li> 29</ol> 30 31</div> 32</div> 33 34<p>In Android N, Android TV users can now watch a video 35in a pinned window in a corner of the screen when navigating within 36apps. Picture-in-picture (PIP) mode lets apps run a video 37activity in the pinned window while another activity continues in the 38background. The PIP window lets users multitask while using your app, which 39helps users be more productive.</p> 40 41<p>Your app can decide when to trigger PIP mode. Here are some examples of 42when to enter PIP mode:</p> 43 44<ul> 45<li>Your app can move a video into PIP mode when the user navigates 46back from the video to browse other content.</li> 47<li>Your app can switch a video into PIP mode while a user watches the end 48of an episode of content. The main screen displays promotional or summary 49information about the next episode in the series.</li> 50<li>Your app can provide a way for users to queue up additional content while 51they watch a video. The video continues playing in PIP mode while the main 52screen displays a content selection activity.</li> 53</ul> 54 55<p>The PIP window is 240x135 dp and is shown at the top-most layer in one of 56the four corners of the screen, chosen by the system. The user can bring up a 57PIP menu that lets them toggle the PIP window to full-screen, or close the PIP 58window, by holding down the <b>Home</b> button on the remote. If another 59video starts playing on the main screen, the PIP window is automatically 60closed. Users can also close the PIP window through Recents.</p> 61 62<img src="{@docRoot}preview/images/pip-active.png" /> 63<p class="img-caption"><strong>Figure 1.</strong> A Picture-in-picture 64video visible in a corner of the screen while the user browses content 65on the main screen.</p> 66 67<p>PIP leverages the multi-window APIs available in Android N to 68provide the pinned video overlay window. To add PIP to your app, you need to 69register your activities that support PIP, switch your activity to PIP mode as 70needed, and make sure UI elements are hidden and video playback continues when 71the activity is in PIP mode.</p> 72 73<h2 id="declaring">Declaring Your Activity Supports Picture-in-picture</h2> 74 75<p>By default, the system does not automatically support PIP for apps. 76If you want support PIP in your app, register your video 77activity in your manifest by setting 78<code>android:supportsPictureInPicture</code> and 79<code>android:resizeableActivity</code> to <code>true</code>. Also, specify 80that your activity handles layout configuration changes so that your activity 81doesn't relaunch when layout changes occur during PIP mode transitions.</p> 82 83<pre> 84<activity android:name="VideoActivity" 85 android:resizeableActivity="true" 86 android:supportsPictureInPicture="true" 87 android:configChanges= 88 "screenSize|smallestScreenSize|screenLayout|orientation" 89 ... 90</pre> 91 92<p>When registering your activity, keep in mind that in PIP mode, your 93activity is shown in a small overlay window on a TV screen. Video playback 94activities with minimal UI provide the best user experience. Activities that 95contain small UI elements might not provide a good user experience 96when switched to PIP mode, because users can't see details of the UI elements 97in the PIP window.</p> 98 99<h2 id="pip_button">Switching Your Activity to Picture-in-picture</h2> 100 101When you need to switch your activity into PIP mode, call 102<code>Activity.enterPictureInPictureMode()</code>. The following example 103switches to PIP mode when the user selects a dedicated PIP button on a media 104control bar:</p> 105 106<pre> 107@Override 108public void onActionClicked(Action action) { 109 if (action.getId() == R.id.lb_control_picture_in_picture) { 110 getActivity().enterPictureInPictureMode(); 111 return; 112 } 113 ... 114</pre> 115 116<p>Adding a PIP button to your media control bar lets your user easily switch 117to PIP mode while controlling video playback.</p> 118 119<img src="{@docRoot}preview/images/pip-button.png" /> 120<p class="img-caption"><strong>Figure 1.</strong> A Picture-in-picture 121button on a media control bar.</p> 122 123<p>Android N includes a new 124<code>PlaybackControlsRow.PictureInPictureAction</code> class which defines 125control bar PIP actions and uses the PIP icon.</p> 126 127<h2 id="handling_ui">Handling UI During Picture-in-picture</h2> 128 129<p>When your activity enters PIP mode, your activity should only show video 130playback. Remove UI elements before your activity enters PIP, 131and restore these elements when your activity becomes full-screen again. 132Override <code>Activity.onPictureInPictureModeChanged()</code> or 133<code>Fragment.onPictureInPictureModeChanged()</code> and enable or 134disable your UI elements as needed, for example:</p> 135 136<pre> 137@Override 138public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) { 139 if (isInPictureInPictureMode) { 140 // Hide the controls in picture-in-picture mode. 141 ... 142 } else { 143 // Restore the playback UI based on the playback status. 144 ... 145 } 146} 147</pre> 148 149<h2 id="continuing_playback">Continuing Video Playback While in 150Picture-in-picture</h2> 151 152<p>When your activity switches to PIP, the system considers the activity in a 153paused state, and calls your activity's {@link android.app.Activity#onPause 154onPause()} method. Video playback should not be paused and should continue 155playing if the activity is paused due to PIP mode.</p> 156 157<p>In Android N, you should pause and resume video playback when the system 158calls your activity's {@link android.app.Activity#onStop onStop()} and 159{@link android.app.Activity#onStart onStart()}. By doing this, you can avoid 160having to check if your app is in PIP mode in 161{@link android.app.Activity#onPause onPause()} and explicitly 162continuing playback.</p> 163 164<p>If you have to pause playback in your {@link android.app.Activity#onPause 165onPause()} implementation, check for PIP mode by calling {@code 166isInPictureInPictureMode()} and handle playback appropriately, for example:</p> 167 168<pre> 169@Override 170public void onPause() { 171 // If called while in PIP mode, do not pause playback 172 if (isInPictureInPictureMode()) { 173 // Continue playback 174 ... 175 } 176 // If paused but not in PIP, pause playback if necessary 177 ... 178} 179</pre> 180 181<p>When your activity switches out of PIP mode back to full-screen mode, the 182system resumes your activity and calls your 183{@link android.app.Activity#onResume onResume()} method.</p> 184 185<h2 id="single_playback">Using a Single Playback Activity for 186Picture-in-picture</h2> 187 188<p>In your app, a user might select a new video when browsing for content on 189the main screen, while a video playback activity is in PIP mode. Play the new 190video in the existing playback activity in full screen mode, instead of 191launching a new activity that might confuse the user.</p> 192 193<p>To ensure a single activity is used for video playback requests and 194switched into or out of PIP mode as needed, set the activity's 195<code>android:launchMode</code> to <code>singleTask</code> in your manifest: 196</p> 197 198<pre> 199<activity android:name="VideoActivity" 200 ... 201 android:supportsPictureInPicture="true" 202 android:launchMode="singleTask" 203 ... 204</pre> 205 206<p>In your activity, override {@link android.app.Activity#onNewIntent 207Activity.onNewIntent()} and handle the new video, stopping any existing video 208playback if needed.</p> 209 210<h2 id="best">Best Practices</h2> 211 212<p>PIP is intended for activities that play full-screen video. When switching 213your activity into PIP mode, avoid showing anything except video content. 214Track when your activity enters PIP mode and hide UI elements, as described 215in <a href="#handling_ui">Handling UI During Picture-in-picture</a>.</p> 216 217<p>Since the PIP window is shown as a floating window in the corner of the 218screen, you should avoid showing critical information in the main screen 219in any area that can be obscured by the PIP window.</p> 220 221<p>When an activity is in PIP mode, by default it doesn't get input focus. To 222receive input events while in PIP mode, use 223{@link android.media.session.MediaSession#setCallback 224MediaSession.setCallback()}. For more information on using 225{@link android.media.session.MediaSession#setCallback setCallback()} see 226<a href="{@docRoot}training/tv/playback/now-playing.html">Displaying 227a Now Playing Card</a>.</p> 228 229<p>When your app is in PIP mode, video playback in the PIP window can cause 230audio interference with another app, such as a music player app or voice search 231app. To avoid this, request audio focus when you start playing the video, 232and handle audio focus change notifications, as described in 233<a href="{@docRoot}training/managing-audio/audio-focus.html">Managing Audio 234Focus</a>. If you receive notification of audio focus loss when in PIP mode, 235pause or stop video playback.</p> 236