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
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.
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
VPlayerItemthat represents the video.
- Take the player item and place it inside an instance of
- 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.statusproperty of your player and wait until the status becomes
ReadyToPlay, at which point you create an instance of the
- Start a KVO listener on the
pictureInPicturePossibleproperty of your controller. Once this value becomes
true, 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 ...