blob: e48ae48d3f4e6754c3da23210cbd939f480f4599 [file] [log] [blame]
Daniel Yue0fa75c2016-07-19 13:45:38 -07001page.title=Adding Picture-in-picture
Daniel Yu192d56d2016-02-23 16:43:04 -08002page.keywords=preview,sdk,PIP,Picture-in-picture
3page.tags=androidn
Daniel Yue0fa75c2016-07-19 13:45:38 -07004helpoutsWidget=true
5
6trainingnavtop=true
Daniel Yu192d56d2016-02-23 16:43:04 -08007
8@jd:body
9
Daniel Yue0fa75c2016-07-19 13:45:38 -070010<div id="tb-wrapper">
11<div id="tb">
Daniel Yud78890f2016-03-04 11:16:46 -080012
13<h2>In this document</h2>
14<ol>
15 <li><a href="#declaring">Declaring Your Activity Supports
Daniel Yu192d56d2016-02-23 16:43:04 -080016Picture-in-picture</a></li>
Daniel Yud78890f2016-03-04 11:16:46 -080017 <li><a href="#pip_button">Switching Your Activity to Picture-in-picture</a>
18</li>
19 <li><a href="#handling_ui">Handling UI During Picture-in-picture</a>
20</li>
21 <li><a href="#continuing_playback">Continuing Video Playback While in
22Picture-in-picture</a></li>
Daniel Yuc5d9c8f2016-03-21 16:41:26 -070023 <li><a href="#single_playback">Using a Single Playback Activity for
24Picture-in-picture</a></li>
Daniel Yud78890f2016-03-04 11:16:46 -080025 <li><a href="#best">Best Practices</a></li>
26</ol>
27
28<h2>See Also</h2>
29<ol>
30 <li><a href="{@docRoot}preview/features/multi-window.html">Multi-Window
31Support</a></li>
32</ol>
33
Daniel Yu192d56d2016-02-23 16:43:04 -080034</div>
35</div>
36
Daniel Yue0fa75c2016-07-19 13:45:38 -070037<p>In Android 7.0, Android TV users can now watch a video
38in a pinned window in a corner of the screen when navigating within or
39between apps. Picture-in-picture (PIP) mode lets apps run a video
Daniel Yu192d56d2016-02-23 16:43:04 -080040activity in the pinned window while another activity continues in the
Daniel Yue0fa75c2016-07-19 13:45:38 -070041background. The PIP window lets users multitask while using Android TV,
42which helps users be more productive.</p>
Daniel Yu192d56d2016-02-23 16:43:04 -080043
Daniel Yud78890f2016-03-04 11:16:46 -080044<p>Your app can decide when to trigger PIP mode. Here are some examples of
Daniel Yu192d56d2016-02-23 16:43:04 -080045when to enter PIP mode:</p>
46
47<ul>
Daniel Yud78890f2016-03-04 11:16:46 -080048<li>Your app can move a video into PIP mode when the user navigates
49back from the video to browse other content.</li>
50<li>Your app can switch a video into PIP mode while a user watches the end
51of an episode of content. The main screen displays promotional or summary
Daniel Yu192d56d2016-02-23 16:43:04 -080052information about the next episode in the series.</li>
Daniel Yud78890f2016-03-04 11:16:46 -080053<li>Your app can provide a way for users to queue up additional content while
54they watch a video. The video continues playing in PIP mode while the main
55screen displays a content selection activity.</li>
Daniel Yu192d56d2016-02-23 16:43:04 -080056</ul>
57
Daniel Yud78890f2016-03-04 11:16:46 -080058<p>The PIP window is 240x135 dp and is shown at the top-most layer in one of
Daniel Yu192d56d2016-02-23 16:43:04 -080059the four corners of the screen, chosen by the system. The user can bring up a
Daniel Yud78890f2016-03-04 11:16:46 -080060PIP menu that lets them toggle the PIP window to full-screen, or close the PIP
Daniel Yu192d56d2016-02-23 16:43:04 -080061window, by holding down the <b>Home</b> button on the remote. If another
Daniel Yud78890f2016-03-04 11:16:46 -080062video starts playing on the main screen, the PIP window is automatically
Daniel Yue0fa75c2016-07-19 13:45:38 -070063closed.</p>
Daniel Yud78890f2016-03-04 11:16:46 -080064
Daniel Yue0fa75c2016-07-19 13:45:38 -070065<img src="{@docRoot}images/android-7.0/pip-active.png" />
Daniel Yud78890f2016-03-04 11:16:46 -080066<p class="img-caption"><strong>Figure 1.</strong> A Picture-in-picture
67video visible in a corner of the screen while the user browses content
68on the main screen.</p>
Daniel Yu192d56d2016-02-23 16:43:04 -080069
Daniel Yue0fa75c2016-07-19 13:45:38 -070070<p>PIP leverages the multi-window APIs available in Android 7.0 to
Daniel Yu192d56d2016-02-23 16:43:04 -080071provide the pinned video overlay window. To add PIP to your app, you need to
72register your activities that support PIP, switch your activity to PIP mode as
73needed, and make sure UI elements are hidden and video playback continues when
74the activity is in PIP mode.</p>
75
76<h2 id="declaring">Declaring Your Activity Supports Picture-in-picture</h2>
77
Daniel Yud78890f2016-03-04 11:16:46 -080078<p>By default, the system does not automatically support PIP for apps.
79If you want support PIP in your app, register your video
Daniel Yu192d56d2016-02-23 16:43:04 -080080activity in your manifest by setting
81<code>android:supportsPictureInPicture</code> and
82<code>android:resizeableActivity</code> to <code>true</code>. Also, specify
83that your activity handles layout configuration changes so that your activity
Daniel Yud78890f2016-03-04 11:16:46 -080084doesn't relaunch when layout changes occur during PIP mode transitions.</p>
Daniel Yu192d56d2016-02-23 16:43:04 -080085
86<pre>
87&lt;activity android:name="VideoActivity"
88 android:resizeableActivity="true"
89 android:supportsPictureInPicture="true"
90 android:configChanges=
91 "screenSize|smallestScreenSize|screenLayout|orientation"
92 ...
93</pre>
94
95<p>When registering your activity, keep in mind that in PIP mode, your
96activity is shown in a small overlay window on a TV screen. Video playback
Daniel Yud78890f2016-03-04 11:16:46 -080097activities with minimal UI provide the best user experience. Activities that
98contain small UI elements might not provide a good user experience
99when switched to PIP mode, because users can't see details of the UI elements
100in the PIP window.</p>
Daniel Yu192d56d2016-02-23 16:43:04 -0800101
102<h2 id="pip_button">Switching Your Activity to Picture-in-picture</h2>
103
104When you need to switch your activity into PIP mode, call
Daniel Yue0fa75c2016-07-19 13:45:38 -0700105{@link android.app.Activity#enterPictureInPictureMode
106enterPictureInPictureMode()}. The following example
Daniel Yu82f87562016-04-05 13:46:49 -0700107switches to PIP mode when the user selects a dedicated PIP button on a media
108control bar:</p>
Daniel Yu192d56d2016-02-23 16:43:04 -0800109
110<pre>
111&#64;Override
112public void onActionClicked(Action action) {
113 if (action.getId() == R.id.lb_control_picture_in_picture) {
Daniel Yu82f87562016-04-05 13:46:49 -0700114 getActivity().enterPictureInPictureMode();
Daniel Yu192d56d2016-02-23 16:43:04 -0800115 return;
116 }
117 ...
118</pre>
119
120<p>Adding a PIP button to your media control bar lets your user easily switch
121to PIP mode while controlling video playback.</p>
122
Daniel Yue0fa75c2016-07-19 13:45:38 -0700123<img src="{@docRoot}images/android-7.0/pip-button.png" />
Daniel Yu192d56d2016-02-23 16:43:04 -0800124<p class="img-caption"><strong>Figure 1.</strong> A Picture-in-picture
125button on a media control bar.</p>
126
Daniel Yue0fa75c2016-07-19 13:45:38 -0700127<p>Android 7.0 includes a
128{@link android.support.v17.leanback.widget.PlaybackControlsRow.PictureInPictureAction
129PlaybackControlsRow.PictureInPictureAction} class which defines
Daniel Yud78890f2016-03-04 11:16:46 -0800130control bar PIP actions and uses the PIP icon.</p>
Daniel Yu192d56d2016-02-23 16:43:04 -0800131
132<h2 id="handling_ui">Handling UI During Picture-in-picture</h2>
133
134<p>When your activity enters PIP mode, your activity should only show video
Daniel Yud78890f2016-03-04 11:16:46 -0800135playback. Remove UI elements before your activity enters PIP,
Daniel Yu192d56d2016-02-23 16:43:04 -0800136and restore these elements when your activity becomes full-screen again.
Daniel Yue0fa75c2016-07-19 13:45:38 -0700137Override {@link android.app.Activity#onPictureInPictureModeChanged
138Activity.onPictureInPictureModeChanged()} or
139{@link android.app.Fragment#onPictureInPictureModeChanged
140Fragment.onPictureInPictureModeChanged()} and enable or
Daniel Yu192d56d2016-02-23 16:43:04 -0800141disable your UI elements as needed, for example:</p>
142
143<pre>
144&#64;Override
Daniel Yu82f87562016-04-05 13:46:49 -0700145public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
146 if (isInPictureInPictureMode) {
Daniel Yu192d56d2016-02-23 16:43:04 -0800147 // Hide the controls in picture-in-picture mode.
148 ...
149 } else {
150 // Restore the playback UI based on the playback status.
151 ...
152 }
153}
154</pre>
155
156<h2 id="continuing_playback">Continuing Video Playback While in
157Picture-in-picture</h2>
158
159<p>When your activity switches to PIP, the system considers the activity in a
Daniel Yu83b25f72016-06-22 09:49:34 -0700160paused state, and calls your activity's {@link android.app.Activity#onPause
161onPause()} method. Video playback should not be paused and should continue
162playing if the activity is paused due to PIP mode.</p>
163
Daniel Yue0fa75c2016-07-19 13:45:38 -0700164<p>In Android 7.0, you should pause and resume video playback when the system
Daniel Yu83b25f72016-06-22 09:49:34 -0700165calls your activity's {@link android.app.Activity#onStop onStop()} and
166{@link android.app.Activity#onStart onStart()}. By doing this, you can avoid
167having to check if your app is in PIP mode in
168{@link android.app.Activity#onPause onPause()} and explicitly
169continuing playback.</p>
170
171<p>If you have to pause playback in your {@link android.app.Activity#onPause
172onPause()} implementation, check for PIP mode by calling {@code
173isInPictureInPictureMode()} and handle playback appropriately, for example:</p>
Daniel Yu192d56d2016-02-23 16:43:04 -0800174
175<pre>
176&#64;Override
177public void onPause() {
Daniel Yu82f87562016-04-05 13:46:49 -0700178 // If called while in PIP mode, do not pause playback
179 if (isInPictureInPictureMode()) {
Daniel Yu192d56d2016-02-23 16:43:04 -0800180 // Continue playback
181 ...
182 }
183 // If paused but not in PIP, pause playback if necessary
184 ...
185}
186</pre>
187
Daniel Yud78890f2016-03-04 11:16:46 -0800188<p>When your activity switches out of PIP mode back to full-screen mode, the
Daniel Yu83b25f72016-06-22 09:49:34 -0700189system resumes your activity and calls your
190{@link android.app.Activity#onResume onResume()} method.</p>
Daniel Yu192d56d2016-02-23 16:43:04 -0800191
Daniel Yuc5d9c8f2016-03-21 16:41:26 -0700192<h2 id="single_playback">Using a Single Playback Activity for
193Picture-in-picture</h2>
194
195<p>In your app, a user might select a new video when browsing for content on
196the main screen, while a video playback activity is in PIP mode. Play the new
197video in the existing playback activity in full screen mode, instead of
198launching a new activity that might confuse the user.</p>
199
200<p>To ensure a single activity is used for video playback requests and
201switched into or out of PIP mode as needed, set the activity's
202<code>android:launchMode</code> to <code>singleTask</code> in your manifest:
203</p>
204
205<pre>
206&lt;activity android:name="VideoActivity"
207 ...
208 android:supportsPictureInPicture="true"
209 android:launchMode="singleTask"
210 ...
211</pre>
212
213<p>In your activity, override {@link android.app.Activity#onNewIntent
Daniel Yue0fa75c2016-07-19 13:45:38 -0700214onNewIntent()} and handle the new video, stopping any existing video
Daniel Yuc5d9c8f2016-03-21 16:41:26 -0700215playback if needed.</p>
216
Daniel Yu192d56d2016-02-23 16:43:04 -0800217<h2 id="best">Best Practices</h2>
218
219<p>PIP is intended for activities that play full-screen video. When switching
220your activity into PIP mode, avoid showing anything except video content.
221Track when your activity enters PIP mode and hide UI elements, as described
Daniel Yud78890f2016-03-04 11:16:46 -0800222in <a href="#handling_ui">Handling UI During Picture-in-picture</a>.</p>
Daniel Yu192d56d2016-02-23 16:43:04 -0800223
224<p>Since the PIP window is shown as a floating window in the corner of the
225screen, you should avoid showing critical information in the main screen
226in any area that can be obscured by the PIP window.</p>
227
228<p>When an activity is in PIP mode, by default it doesn't get input focus. To
229receive input events while in PIP mode, use
Daniel Yu83b25f72016-06-22 09:49:34 -0700230{@link android.media.session.MediaSession#setCallback
231MediaSession.setCallback()}. For more information on using
232{@link android.media.session.MediaSession#setCallback setCallback()} see
233<a href="{@docRoot}training/tv/playback/now-playing.html">Displaying
234a Now Playing Card</a>.</p>
235
236<p>When your app is in PIP mode, video playback in the PIP window can cause
237audio interference with another app, such as a music player app or voice search
238app. To avoid this, request audio focus when you start playing the video,
239and handle audio focus change notifications, as described in
240<a href="{@docRoot}training/managing-audio/audio-focus.html">Managing Audio
241Focus</a>. If you receive notification of audio focus loss when in PIP mode,
242pause or stop video playback.</p>