Chapter 7. Multitasking
iOS 9 added some really cool multitasking functionalities to select devices, such as the latest iPads. One of these functionalities is PiP, or Picture in Picture. In this chapter, we’ll have a look at some of these exciting features.
7.1 Adding Picture in Picture Playback Functionality
Problem
You want to let a user shrink a video to occupy a portion of the screen, so that she can view and interact with other content in other apps.
Solution
I’ll break the process down into small and digestible steps:
- You need a view that has a layer of type
AVPlayerLayer
. This layer will be used by a view controller to display the video. - Instantiate an item of type
VPlayerItem
that represents the video. - Take the player item and place it inside an instance of
AVPlayer
. - Assign this player to your view’s layer player object. (Don’t worry if this sounds confusing. I’ll explain it soon.)
- Assign this view to your view controller’s main view and issue the
play()
function on the player to start normal playback. - Using KVO, listen to changes to the
currentItem.status
property of your player and wait until the status becomesReadyToPlay
, at which point you create an instance of theAVPictureInPictureController
class. - Start a KVO listener on the
pictureInPicturePossible
property of your controller. Once this value becomestrue
, let the user know that she can now go into Picture in Picture mode. - Now when the user presses a button to start Picture in Picture, read the value of ...
Get iOS 9 Swift Programming Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.