Cara Picture in Picture YouTube Samsung

Starting in Android 8.0 (API level 26), Android allows activities to launch in picture-in-picture (PiP) mode. PiP is a special type of multi-window mode mostly used for video playback. It lets the user watch a video in a small window pinned to a corner of the screen while navigating between apps or browsing content on the main screen.

Alas, your browser doesn't support HTML5 video. That's OK! You can still download the video and watch it with a video player.

PiP leverages the multi-window APIs made available in Android 7.0 to provide the pinned video overlay window. To add PiP to your app, you need to register your activities that support PiP, switch your activity to PiP mode as needed, and make sure UI elements are hidden and video playback continues when the activity is in PiP mode.

The PiP window appears in the topmost layer of the screen, in a corner chosen by the system.

How users can interact with the PiP window

Users can drag the PiP window to another location. Starting in Android 12, users can also:

  • Single-tap the window to display a full-screen toggle, a close button, a settings button, and custom actions provided by your app (for example, play controls).

  • Double-tap the window to toggle between the current PiP size and maximum PiP size.

  • Stash the window by dragging it to the left or right edge. To unstash the window, either tap the visible part of the stashed window or drag it out.

  • Resize the PiP window using pinch-to-zoom.

Your app controls when the current activity enters PiP mode. Here are some examples:

  • An activity can enter PiP mode when the user taps the home button or swipes up to home. This is how Google Maps continues to display directions while the user runs another activity at the same time.

  • Your app can move a video into PiP mode when the user navigates back from the video to browse other content.

  • Your app can switch a video into PiP mode while a user watches the end of an episode of content. The main screen displays promotional or summary information about the next episode in the series.

  • Your app can provide a way for users to queue up additional content while they watch a video. The video continues playing in PiP mode while the main screen displays a content selection activity.

Declare picture-in-picture support

By default, the system does not automatically support PiP for apps. If you want support PiP in your app, register your video activity in your manifest by setting override fun onActionClicked(action: Action) { if (action.id.toInt() == R.id.lb_control_picture_in_picture) { activity?.enterPictureInPictureMode() return } } 4 to override fun onActionClicked(action: Action) { if (action.id.toInt() == R.id.lb_control_picture_in_picture) { activity?.enterPictureInPictureMode() return } } 5. Also, specify that your activity handles layout configuration changes so that your activity doesn't relaunch when layout changes occur during PiP mode transitions.

<activity android:name="VideoActivity" android:supportsPictureInPicture="true" android:configChanges= "screenSize|smallestScreenSize|screenLayout|orientation" ...

Switch your activity to picture-in-picture

To enter picture-in-picture mode, an activity must call override fun onActionClicked(action: Action) { if (action.id.toInt() == R.id.lb_control_picture_in_picture) { activity?.enterPictureInPictureMode() return } } 6. For example, the following code switches an activity to PiP mode when the user clicks a dedicated button in the app's UI:

Kotlin

override fun onActionClicked(action: Action) { if (action.id.toInt() == R.id.lb_control_picture_in_picture) { activity?.enterPictureInPictureMode() return } }

Java

@Override public void onActionClicked(Action action) { if (action.getId() == R.id.lb_control_picture_in_picture) { getActivity().enterPictureInPictureMode(); return; } ... }

You might want to include logic that switches an activity into PiP mode instead of going into the background. For example, Google Maps switches to PiP mode if the user presses the home or recents button while the app is navigating. You can catch this case by overriding override fun onActionClicked(action: Action) { if (action.id.toInt() == R.id.lb_control_picture_in_picture) { activity?.enterPictureInPictureMode() return } } 7:

Kotlin

override fun onUserLeaveHint() { if (iWantToBeInPipModeNow()) { enterPictureInPictureMode() } }

Java

@Override public void onUserLeaveHint () { if (iWantToBeInPipModeNow()) { enterPictureInPictureMode(); } }

Make transitions to PiP mode smoother from gesture navigation

Starting in Android 12, you can use the override fun onActionClicked(action: Action) { if (action.id.toInt() == R.id.lb_control_picture_in_picture) { activity?.enterPictureInPictureMode() return } } 8 flag to provide smoother transitions to PiP mode when swiping up to home in gesture navigation mode:

  1. Use override fun onActionClicked(action: Action) { if (action.id.toInt() == R.id.lb_control_picture_in_picture) { activity?.enterPictureInPictureMode() return } } 8 to construct @Override public void onActionClicked(Action action) { if (action.getId() == R.id.lb_control_picture_in_picture) { getActivity().enterPictureInPictureMode(); return; } ... } 0:

    setPictureInPictureParams(new PictureInPictureParams.Builder() .setAspectRatio(aspectRatio) .setSourceRectHint(sourceRectHint) .setAutoEnterEnabled(true) .build()); Note: When override fun onActionClicked(action: Action) { if (action.id.toInt() == R.id.lb_control_picture_in_picture) { activity?.enterPictureInPictureMode() return } } 8 is enabled, you don’t need to explicitly call @Override public void onActionClicked(Action action) { if (action.getId() == R.id.lb_control_picture_in_picture) { getActivity().enterPictureInPictureMode(); return; } ... } 2 in @Override public void onActionClicked(Action action) { if (action.getId() == R.id.lb_control_picture_in_picture) { getActivity().enterPictureInPictureMode(); return; } ... } 3.
  2. Call @Override public void onActionClicked(Action action) { if (action.getId() == R.id.lb_control_picture_in_picture) { getActivity().enterPictureInPictureMode(); return; } ... } 4 with the up-to-date @Override public void onActionClicked(Action action) { if (action.getId() == R.id.lb_control_picture_in_picture) { getActivity().enterPictureInPictureMode(); return; } ... } 5 early. The app should not wait for the @Override public void onActionClicked(Action action) { if (action.getId() == R.id.lb_control_picture_in_picture) { getActivity().enterPictureInPictureMode(); return; } ... } 3 callback (as it would have done in Android 11).

    For example, an app may want to call @Override public void onActionClicked(Action action) { if (action.getId() == R.id.lb_control_picture_in_picture) { getActivity().enterPictureInPictureMode(); return; } ... } 4 on the very first playback and any following playback if the aspect ratio is changed.

  3. Call @Override public void onActionClicked(Action action) { if (action.getId() == R.id.lb_control_picture_in_picture) { getActivity().enterPictureInPictureMode(); return; } ... } 8 as needed. For example, it’s probably not optimal for a video app to enter PiP if the current playback is in paused state.

Handle UI during picture-in-picture

When the activity enters or exits picture-in-picture mode, the system calls @Override public void onActionClicked(Action action) { if (action.getId() == R.id.lb_control_picture_in_picture) { getActivity().enterPictureInPictureMode(); return; } ... } 9 or override fun onUserLeaveHint() { if (iWantToBeInPipModeNow()) { enterPictureInPictureMode() } } 0.

You should override these callbacks to redraw the activity's UI elements. Keep in mind that in PiP mode your activity is shown in a small window. Users cannot interact with your app's UI elements when it's in PiP mode and the details of small UI elements may be difficult to see. Video playback activities with minimal UI provide the best user experience.

If your app needs to provide custom actions for PiP, see Add controls on this page. Remove other UI elements before your activity enters PiP and restore them when your activity becomes fullscreen again:

Kotlin

override fun onPictureInPictureModeChanged(isInPictureInPictureMode: Boolean, newConfig: Configuration) { if (isInPictureInPictureMode) { // Hide the full-screen UI (controls, etc.) while in picture-in-picture mode. } else { // Restore the full-screen UI. } }

Java

@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. ... } }

Support smoother animations when exiting out of PiP mode

Starting in Android 12, the override fun onUserLeaveHint() { if (iWantToBeInPipModeNow()) { enterPictureInPictureMode() } } 1 flag is now reused to implement smoother animation when exiting out of PiP mode. On exit, the system creates the animation using the current available override fun onUserLeaveHint() { if (iWantToBeInPipModeNow()) { enterPictureInPictureMode() } } 2, whether it’s the original override fun onUserLeaveHint() { if (iWantToBeInPipModeNow()) { enterPictureInPictureMode() } } 3 used to enter PiP or an updated override fun onUserLeaveHint() { if (iWantToBeInPipModeNow()) { enterPictureInPictureMode() } } 3 provided by the app.

To implement this feature, update your app as follows:

  1. Continue to construct @Override public void onActionClicked(Action action) { if (action.getId() == R.id.lb_control_picture_in_picture) { getActivity().enterPictureInPictureMode(); return; } ... } 5 with the override fun onUserLeaveHint() { if (iWantToBeInPipModeNow()) { enterPictureInPictureMode() } } 2 and override fun onUserLeaveHint() { if (iWantToBeInPipModeNow()) { enterPictureInPictureMode() } } 7 for a smooth entry animation.

  2. If necessary, update the override fun onUserLeaveHint() { if (iWantToBeInPipModeNow()) { enterPictureInPictureMode() } } 2 before the system starts the exit transition. When the system is about to exit PiP mode, the activity’s view hierarchy is laid out to its destination configuration (for example, full screen). The app can attach a layout change listener to its root view or target view (such as the video player view) to detect the event and update the sourceRectHint before the animation begins.

    Kotlin

    // Listener is called immediately after the user exits PiP but before animating. playerView.addOnLayoutChangeListener { _, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom -> if (left != oldLeft || right != oldRight || top != oldTop || bottom != oldBottom) { // The playerView's bounds changed, update the source hint rect to // reflect its new bounds. val sourceRectHint = Rect() playerView.getGlobalVisibleRect(sourceRectHint) setPictureInPictureParams( PictureInPictureParams.Builder() .setSourceRectHint(sourceRectHint) .build() ) } }

    Java

    // Listener is called right after the user exits PiP but before // animating. playerView.addOnLayoutChangeListener((v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> { if (left != oldLeft || right != oldRight || top != oldTop || bottom != oldBottom) { // The playerView’s bounds changed, update the source hint rect to // reflect its new bounds. final Rect sourceRectHint = new Rect(); playerView.getGlobalVisibleRect(sourceRectHint); setPictureInPictureParams( new PictureInPictureParams.Builder() .setSourceRectHint(sourceRectHint) .build()); } });

Add controls

The PiP window can display controls when the user opens the window's menu (by tapping the window on a mobile device, or selecting the menu from the TV remote.)

If an app has an active media session, then play, pause, next, and previous controls will appear.

You can also specify custom actions explicitly by building @Override public void onActionClicked(Action action) { if (action.getId() == R.id.lb_control_picture_in_picture) { getActivity().enterPictureInPictureMode(); return; } ... } 5 with @Override public void onUserLeaveHint () { if (iWantToBeInPipModeNow()) { enterPictureInPictureMode(); } } 0 before entering PiP mode, and pass the params when you enter PiP mode using @Override public void onUserLeaveHint () { if (iWantToBeInPipModeNow()) { enterPictureInPictureMode(); } } 1 or @Override public void onUserLeaveHint () { if (iWantToBeInPipModeNow()) { enterPictureInPictureMode(); } } 2. Be careful. If you try to add more than @Override public void onUserLeaveHint () { if (iWantToBeInPipModeNow()) { enterPictureInPictureMode(); } } 3, you'll only get the maximum number.

Disable seamless resizing for non-video content

Android 12 adds the @Override public void onUserLeaveHint () { if (iWantToBeInPipModeNow()) { enterPictureInPictureMode(); } } 4 flag, which provides a much smoother cross-fading animation when resizing non-video content in the PiP window. Previously, resizing non-video content in a PiP window could create jarring visual artifacts.

The @Override public void onUserLeaveHint () { if (iWantToBeInPipModeNow()) { enterPictureInPictureMode(); } } 4 flag is set to override fun onActionClicked(action: Action) { if (action.id.toInt() == R.id.lb_control_picture_in_picture) { activity?.enterPictureInPictureMode() return } } 5 by default for backward-compatibility. Leave this set to override fun onActionClicked(action: Action) { if (action.id.toInt() == R.id.lb_control_picture_in_picture) { activity?.enterPictureInPictureMode() return } } 5 for video content, and change it to @Override public void onUserLeaveHint () { if (iWantToBeInPipModeNow()) { enterPictureInPictureMode(); } } 8 for non-video content.

To disable seamless resizing for non-video content:

override fun onActionClicked(action: Action) { if (action.id.toInt() == R.id.lb_control_picture_in_picture) { activity?.enterPictureInPictureMode() return } } 0

Continuing video playback while in picture-in-picture

When your activity switches to PiP, the system places the activity in the paused state and calls the activity's @Override public void onUserLeaveHint () { if (iWantToBeInPipModeNow()) { enterPictureInPictureMode(); } } 9 method. Video playback should not be paused and should continue playing if the activity is paused while in PiP mode.

In Android 7.0 and later, you should pause and resume video playback when the system calls your activity's setPictureInPictureParams(new PictureInPictureParams.Builder() .setAspectRatio(aspectRatio) .setSourceRectHint(sourceRectHint) .setAutoEnterEnabled(true) .build()); 0 and setPictureInPictureParams(new PictureInPictureParams.Builder() .setAspectRatio(aspectRatio) .setSourceRectHint(sourceRectHint) .setAutoEnterEnabled(true) .build()); 1. By doing this, you can avoid having to check if your app is in PiP mode in onPause() and explicitly continuing playback.

If you have to pause playback in your @Override public void onUserLeaveHint () { if (iWantToBeInPipModeNow()) { enterPictureInPictureMode(); } } 9 implementation, check for PiP mode by calling setPictureInPictureParams(new PictureInPictureParams.Builder() .setAspectRatio(aspectRatio) .setSourceRectHint(sourceRectHint) .setAutoEnterEnabled(true) .build()); 3 and handle playback appropriately, for example:

Kotlin

override fun onActionClicked(action: Action) { if (action.id.toInt() == R.id.lb_control_picture_in_picture) { activity?.enterPictureInPictureMode() return } } 1

Java

override fun onActionClicked(action: Action) { if (action.id.toInt() == R.id.lb_control_picture_in_picture) { activity?.enterPictureInPictureMode() return } } 2

When your activity switches out of PiP mode back to full-screen mode, the system resumes your activity and calls your setPictureInPictureParams(new PictureInPictureParams.Builder() .setAspectRatio(aspectRatio) .setSourceRectHint(sourceRectHint) .setAutoEnterEnabled(true) .build()); 4 method.

Use a single playback activity for picture-in-picture

In your app, a user might select a new video when browsing for content on the main screen, while a video playback activity is in PiP mode. Play the new video in the existing playback activity in full screen mode, instead of launching a new activity that might confuse the user.

To ensure a single activity is used for video playback requests and switched into or out of PiP mode as needed, set the activity's setPictureInPictureParams(new PictureInPictureParams.Builder() .setAspectRatio(aspectRatio) .setSourceRectHint(sourceRectHint) .setAutoEnterEnabled(true) .build()); 5 to setPictureInPictureParams(new PictureInPictureParams.Builder() .setAspectRatio(aspectRatio) .setSourceRectHint(sourceRectHint) .setAutoEnterEnabled(true) .build()); 6 in your manifest:

override fun onActionClicked(action: Action) { if (action.id.toInt() == R.id.lb_control_picture_in_picture) { activity?.enterPictureInPictureMode() return } } 3

In your activity, override setPictureInPictureParams(new PictureInPictureParams.Builder() .setAspectRatio(aspectRatio) .setSourceRectHint(sourceRectHint) .setAutoEnterEnabled(true) .build()); 7 and handle the new video, stopping any existing video playback if needed.

Best practices

PiP might be disabled on devices that have low RAM. Before your app uses PiP, check to be sure it is available by calling setPictureInPictureParams(new PictureInPictureParams.Builder() .setAspectRatio(aspectRatio) .setSourceRectHint(sourceRectHint) .setAutoEnterEnabled(true) .build()); 8.

PiP is intended for activities that play full-screen video. When switching your activity into PiP mode, avoid showing anything except video content. Track when your activity enters PiP mode and hide UI elements, as described in Handling UI during picture-in-picture.

When an activity is in PiP mode, by default it doesn't get input focus. To receive input events while in PiP mode, use setPictureInPictureParams(new PictureInPictureParams.Builder() .setAspectRatio(aspectRatio) .setSourceRectHint(sourceRectHint) .setAutoEnterEnabled(true) .build()); 9. For more information on using override fun onPictureInPictureModeChanged(isInPictureInPictureMode: Boolean, newConfig: Configuration) { if (isInPictureInPictureMode) { // Hide the full-screen UI (controls, etc.) while in picture-in-picture mode. } else { // Restore the full-screen UI. } } 0 see Display a Now Playing card.

When your app is in PiP mode, video playback in the PiP window can cause audio interference with another app, such as a music player app or voice search app. To avoid this, request audio focus when you start playing the video, and handle audio focus change notifications, as described in Managing Audio Focus. If you receive notification of audio focus loss when in PiP mode, pause or stop video playback.

When your app is about to enter PiP, note only the top activity will enter Picture-in-Picture. In some situations such as on multi-window devices, it is possible the activity below will now be shown and become visible again alongside the PiP activity. You should handle this case accordingly, including the activity below getting an setPictureInPictureParams(new PictureInPictureParams.Builder() .setAspectRatio(aspectRatio) .setSourceRectHint(sourceRectHint) .setAutoEnterEnabled(true) .build()); 4 or an @Override public void onUserLeaveHint () { if (iWantToBeInPipModeNow()) { enterPictureInPictureMode(); } } 9 callback. It is also possible that the user may interact with the activity. For example, if you have a video list activity displayed and the playing video activity in PiP mode, the user might select a new video from the list and the PiP activity should update accordingly.

Additional sample code

To download a sample app written in Android, see Picture-in-Picture Sample. To download a sample app written in Kotlin, see Android PictureInPicture Sample (Kotlin).

Bagaimana cara mengaktifkan Picture in Picture YouTube?

MENGAKTIFKAN picture-in-picture.
Buka setelan Android Aplikasi & notifikasi Lanjutan Akses aplikasi khusus. Picture-in-picture..
Ketuk YouTube..
Untuk mengaktifkan, ketuk Izinkan picture-in-picture..
Buka setelan aplikasi YouTube. Umum..
Alihkan tombol Picture-in-Picture ke aktif ..

Picture in Picture android berapa?

Mulai dari Android 8.0 (API level 26), Android memungkinkan aktivitas diluncurkan dalam mode picture-in-picture (PiP).

Dimana letak fitur PiP?

Caranya sebagai berikut. Buka setelan Android > Aplikasi & notifikasi > Lanjutan > Akses aplikasi khusus > Picture-in-picture. Ketuk "YouTube". Untuk mengaktifkan, ketuk "Izinkan Picture-in-picture".

Bagaimana cara mengaktifkan PiP di WhatsApp?

Selain lewat menu pengaturan yang terletak di halaman fitur perangkat bersama dengan jam dan tanggal, kamu juga bisa mengaktifkan Picture in Picture dengan menekan agak lama pada ikon WhatsApp yang ada di layar. Setelah itu akan muncul pilihan dan ketuk tombol Info Aplikasi di paling kanan.

Postingan terbaru

LIHAT SEMUA