How to implement Picture in Picture (PIP) in Android?


The PiP feature on Android allows users to effortlessly multitask by watching videos or engaging with other content while using other apps simultaneously. This innovative functionality ensures that you're always in control of your device, even while you're enjoying media entertainment. The small floating window containing the video or content enables these activities and enhances the user experience by making tasks easier to manage simultaneously.

To add Picture-in-Picture functionality to an Android app, developers need to utilize specific APIs and functionalities available in the Android framework. PiP enhances user experience by offering seamless multitasking within apps. This comprehensive guide will walk through the necessary steps and techniques needed for implementing PiP in an Android application with ease.

Picture in Picture (PIP)

PiP, also known as Picture-in-Picture, is a handy feature that lets users multitask on their device. They can view and interact with media content in a separate small window or overlay while accessing other applications simultaneously.

Users can now multitask more effectively with the PiP functionality, which allows them to keep watching videos - be it a movie or a video call in a compact and resizable window that stays on top of other app windows.

The PiP window provides ample scope for customization by allowing users to move around the screen, resize the window size as per their convenience and even interact with it. This leads to seamless and uninterrupted consumption of media content while performing other tasks on the device.

Approaches

Implementing Picture-in-Picture (PiP) in an Android application using Java can be done in a variety of ways. This article covers three commonly used approaches to achieve PiP functionality:

  • Using the Picture-in-Picture mode in Media Player

  • Utilizing the Picture-in-Picture mode with a custom video player

  • Utilizing the Picture-in-Picture mode with WebView

Using the Picture-in-Picture mode in Media Player

This method involves creating a MediaSessionCompat object and handling media playback using a MediaPlayer. You set up media controls and actions, register a MediaSessionCompat.Callback to handle PiP callbacks, and use the setPictureInPictureParams() method to enter Picture-in-Picture mode.

Algorithm

  • Create a MediaSessionCompat object and handle media playback using a MediaPlayer or other media player.

  • Set up the media controls and playback actions.

  • Register a MediaSessionCompat.Callback to handle callbacks related to PiP.

  • Use the setPictureInPictureParams() method to enter Picture-in-Picture mode.

Example

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.media.session.MediaSessionCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
   private MediaPlayer mediaPlayer;
   private MediaSessionCompat mediaSession;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      // Create a MediaSessionCompat object
      mediaSession = new MediaSessionCompat(this, "MyMediaSession");

      // Set up media controls and actions
      MediaSessionCompat.Callback callback = new MediaSessionCompat.Callback() {
         @Override
         public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode) {
            if (isInPictureInPictureMode) {
               // Update UI for Picture-in-Picture mode
            } else {
               // Update UI when PiP mode is exited
            }
         }
         // Other media callbacks...
      };
      mediaSession.setCallback(callback);

      // Initialize MediaPlayer
      mediaPlayer = MediaPlayer.create(this, R.raw.video);

      Button pipButton = findViewById(R.id.pip_button);
      pipButton.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            // Enter Picture-in-Picture mode
            PictureInPictureParams.Builder builder = new PictureInPictureParams.Builder();
            enterPictureInPictureMode(builder.build());
         }
      });
   }

   @Override
   protected void onDestroy() {
      super.onDestroy();
      // Release resources
      mediaPlayer.release();
      mediaSession.release();
   }
}

Output

Utilizing the Picture-in-Picture mode with a custom video player

Here, you implement a custom video player using a SurfaceView or TextureView to display the video. You handle video playback and controls within your custom player and manage the transition to PiP mode using the enterPictureInPictureMode() method.

Algorithm

  • Implement a custom video player using a SurfaceView or TextureView to display the video.

  • Handle the video playback and controls in your custom player.

  • Manage the transition to PiP mode using the enterPictureInPictureMode() method.

Example

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Rational;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
   private CustomVideoPlayerView playerView;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      playerView = findViewById(R.id.player_view);

      Button pipButton = findViewById(R.id.pip_button);
      pipButton.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            enterPictureInPictureMode();
         }
      });
   }

   @Override
   protected void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration newConfig) {
      super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);

      if (isInPictureInPictureMode) {
         // Update UI for Picture-in-Picture mode
         playerView.hideControls();
      } else {
         // Update UI when PiP mode is exited
         playerView.showControls();
      }
   }
}

Output

Utilizing the Picture-in-Picture mode with WebView

This method involves loading a video or media content in a WebView using WebView.loadUrl(). You implement the necessary WebView callbacks for video playback, detect when the video should enter PiP mode, and call enterPictureInPictureMode() to initiate the transition to Picture-in-Picture mode.

Algorithm

  • Load a video or media content in a WebView using WebView.loadUrl().

  • Implement the necessary WebView callbacks for video playback.

  • Detect when the video should enter PiP mode and call enterPictureInPictureMode().

Example

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {
   private WebView webView;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      webView = findViewById(R.id.webview);
      webView.setWebChromeClient(new WebChromeClient());
      webView.setWebViewClient(new WebViewClient());
      WebSettings webSettings = webView.getSettings();
      webSettings.setJavaScriptEnabled(true);

      Button pipButton = findViewById(R.id.pip_button);
      pipButton.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            enterPictureInPictureMode();
         }
      });

      webView.loadUrl("https://www.example.com/video");
   }

   @Override
   protected void onStop() {
      super.onStop();
      webView.onPause();
      webView.pauseTimers();
   }

   @Override
   protected void onResume() {
      super.onResume();
      webView.resumeTimers();
      webView.onResume();
   }

   @Override
   protected void onDestroy() {
      super.onDestroy();
      webView.destroy();
   }
}

Output

Conclusion

Developers can use Picture-in-Picture (PiP) to improve user experience in Android. This feature lets users multitask and view media content in a small overlay while using other apps. By implementing PiP with Media Player, custom video player, or WebView methods, developers give users a seamless and convenient multitasking experience. Ultimately, this improves the usability and engagement of their Android applications.

Updated on: 26-Jul-2023

479 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements