Skip to Content
iOS 9 Swift Programming Cookbook
book

iOS 9 Swift Programming Cookbook

by Vandad Nahavandipoor
December 2015
Intermediate to advanced
350 pages
6h 32m
English
O'Reilly Media, Inc.
Content preview from iOS 9 Swift Programming Cookbook

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:

  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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

iOS 10 Swift Programming Cookbook

iOS 10 Swift Programming Cookbook

Vandad Nahavandipoor

Publisher Resources

ISBN: 9781491936689Errata Page