From the course: iPadOS 13 Development Essential Training

Using Picture in Picture - iOS Tutorial

From the course: iPadOS 13 Development Essential Training

Start my 1-month free trial

Using Picture in Picture

- [Instructor] Another key feature in multitasking in iPad OS is being able to do Picture in Picture. To support Picture in Picture, you simply have to play a full screen video and tell your app to support Picture in Picture. So, here I am, with a content view, and I've already imported AVKit, and then I have movie called "tree", and I'm going to head over to my project settings, select my target, and then go to Signing & Capabilities, and then hit "Capability", and from Capability, you want to go to Background Modes, and just double click that to add background modes, and you'll need to support the top one here, which is "Audio, Airplay, and Picture in Picture". Let's go back over to the content view. Now what we're going to do is create an AV player controller. Now that class is not supported by SwiftUI so, we're going too create a custom one as we looked at earlier. Except this is a little bit different, because we're going to implement UIView Controller Representable, rather than UIView Representable. As you'll see here, the process is the same. So create a new struct, called "VideoPlayer", and then this is going to inherit from UIView Controller Representable and want to implement two methods: make UIView Controller and then update UIView Controller. And then here, I'm going to delete the word "some" in both cases. And if we wanted to, we can simply return a new AV Player Controller, but we need to do a little bit more than that to actually play a video. So let's create a constant called "player controller", set it equal to an AV Player View Controller and then we'll return the player controller at the bottom, and then we'll replace our call to create the text object in the the Content View with a call to our video player. It works just like a View in this case, so we can extend the View Controller, or the View and as long as we call that object in SwiftUI, it will display just fine. So, now we need too create an AV Player. To do that, we're going to need a URL for the video. So, what we're going to do is type "if let URL", and we'll have that equal "Bundle.main.url" for Resource, and the Resource is "tree" with the extension "mov" and if that's valid, then we're going to create the player. So, let player equal AV Player, pass in the URL, and then we'll se the Player Controller's player to "player" and that's really all there is to it. At this point, we can just run the application, and then play the movie in full screen and we can exit out and we can see it running in Picture in Picture. So, hit the Run button to run the application. So, here's our movie and I'm going to hit this button right here to minimize it. That brings it in this window here, and if I hit the Home button on the iPad, then I'll see that I have Picture in Picture running. So I could then run another application, and then you still see the video, which can be played and paused. And if I want to go back, simply hit that button to expand it, and there I am back in my application. So, we've added support for Picture in Picture by enabling the appropriate background mode and then in SwiftUI, we've created a custom video player and then we're presenting that in a body of our content view.

Contents