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:

  1. You need a view that has a layer of type AVPlayerLayer. This layer will be used by a view controller to display the video.
  2. Instantiate an item of type VPlayerItem that represents the video.
  3. Take the player item and place it inside an instance of AVPlayer.
  4. Assign this player to your view’s layer player object. (Don’t worry if this sounds confusing. I’ll explain it soon.)
  5. Assign this view to your view controller’s main view and issue the play() function on the player to start normal playback.
  6. Using KVO, listen to changes to the currentItem.status property of your player and wait until the status becomes ReadyToPlay, at which point you create an instance of the AVPictureInPictureController class.
  7. Start a KVO listener on the pictureInPicturePossible property of your controller. Once this value becomes true, let the user know that she can now go into Picture in Picture mode.
  8. Now when the user presses a button to start Picture in Picture, read the value of ...

Get iOS 9 Swift Programming Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.