The new Picture-in-Picture mode lets you shrink a video that's currently running to a corner of the screen. The video can keep playing, while the user can navigate and use other apps. This feature requires a physical device running Android O - it doesn't work on the emulator.
- [Instructor] There is one other code sample available from Google at this point for Android O that demonstrates the new picture in picture feature. This allows you to shrink down a video that you're playing with the media player component and show it in a small corner of the screen. The rest of the device stays active. The user can navigate between apps, but the video can keep on running. Now I'll caution you that you can only test out this feature with an actual physical device. Picture in picture does not work on the Android emulator.
I'll import an Android code sample. And from Android O preview, I'll choose picture in picture. And I'll download it and open up the project. Now, you might notice this one takes a little bit longer to download, and that's because it includes a complete video file. As always, if you're prompted to update your Grado plugin, accept the update. Now before I get into the code, I'm going to run the app on a physical device. I'm using a Google Pixel that's running Android O and I'm projecting the Pixel's screen to my screen.
I'll click the run button and then take a look at the device. When the app opens, the video starts playing right away, and there's a button labeled 'enter picture in picture mode.' I'll press the button on the device and the video shrinks down but keeps on playing. Now while all that's going on, I'll go to my settings, and I'll see that no matter what happens, the video keeps on playing at the bottom. I'll exit all those apps, and then I'll double tap on the video, and the app returns to its full size.
So there's a lot of code at work here. Let's take a look at some of it. The application has a couple of critical classes. There's a main activity class, and under the widget package, there's the movie view class. The main activity class is doing most of the work. I'm going to search for something called picture in picture mode. Now I'll keep on searching until I find this method. enterPIctureInPictureMode. This is the critical new method that's been added to Android O.
And there's a set of arguments that you can pass in. Now I'll press shift + F1 to go to external help and that should open a help page in a browser, and take me to the new enter picture in picture mode method. Notice over here, there's a new category labeled O, and if I reduce that down to API 25, that method is disabled. So this is only available on devices that are running Android O. And if you want to use this feature that still have your app support older versions of Android, you'll need to wrap calls to this method and its associated methods with conditional code, making sure that it's running on the right version of the operating system.
Now again, there's a lot of code to examine here, and I don't have time to go through it in detail, but I will mention that the movie view class encapsulates the critical component that actually displays the video. That's the media player. It's referenced right here at line 95 in this version of the application, and there's a lot of supporting code to manage everything the media player is doing. The picture in picture feature is yet another new tool that you can use on Android O.
- Installing Android Studio 2.4 Preview
- Targeting Android O in a new project
- Setting up Android O testing devices
- Exploring the new features for users
- Using display notifications in channels
- Using Autofill EditText components
- Displaying video as picture-in-picture
- Exploring other new features for developers