Giter Site home page Giter Site logo

sweefties / ios9-newapi-ipad-multitasking-pip-example Goto Github PK

View Code? Open in Web Editor NEW
14.0 4.0 1.0 1.17 MB

iOS 9 Experiments - New API Components - Multitasking on iPad with Picture-in-Picture (PiP).

Swift 100.00%
ipad ipad-multitasking picture-in-picture avplayerviewcontroller ios swift

ios9-newapi-ipad-multitasking-pip-example's Introduction

iOS 9 - New API - iPad Multitasking - PiP Example

iOS 9~ Experiments - New API Components - Multitasking on iPad with Picture-in-Picture (PiP).

Example

Requirements

  • = XCode 8.0.

  • = Swift 3.

  • = iOS 9.0.

  • = iPad Mini 3 / 4, iPad Air / Air 2, iPad Pro

Tested on iOS 9.1 Simulators. 50%

Important

this is the Xcode 8 / Swift updated project.

Sources

WWDC 2015 Sessions from : WWDC 2015 Videos Jamie XX - Loud Places (feat. Romy) from : youtube.com

App Transport Security Settings

To support HTTP hosted : add the Boolean type Value to YES for NSAllowsArbitraryLoads in app's info.plist file.

Usage

  • To run the example project, download or clone the repo.
  • MULTITASKING RECOMMANDATIONS : Perform and prepare your app to avoid memory leaks and others with Instruments!
  • IN USE OF AVPictureInPictureController , use : AVPictureInPictureController.isPictureInPictureSupported() to get devices capability

Example Code!

  • Set your AVPlayer, AVPlayerViewController or AVPlayerLayer in one controller to play video!
  • Add AVKit + AVFoundation Frameworks
import AVKit
import AVFoundation
  • Set AVAudioSession in didFinishLaunchingWithOptions UIApplication Delegate
/// REQUIRED FOR PIP!!!
/// After active Audio/Airplay Background Mode in General Settings.
/// Setup Audio Session for Picture in Picture
let audioSession = AVAudioSession.sharedInstance()

do {
    try audioSession.setCategory(AVAudioSessionCategoryPlayback)
}
catch {
    print("handle errors setting Audio session Category")
}
  • Delegate your controller with AVPlayerViewControllerDelegate Extension
extension ViewController : AVPlayerViewControllerDelegate {

    /// playerViewController will start PIP
    func playerViewControllerWillStartPictureInPicture(playerViewController: AVPlayerViewController) {
        print("PIP will start")
    }

    /// playerViewController did start PIP
    func playerViewControllerDidStartPictureInPicture(playerViewController: AVPlayerViewController) {
        print("PIP did start")
    }

    /// playerViewController will stop PIP
    func playerViewControllerWillStopPictureInPicture(playerViewController: AVPlayerViewController) {
        print("PIP will stop")
    }

    /// playerViewController did stop PIP
    func playerViewControllerDidStopPictureInPicture(playerViewController: AVPlayerViewController) {
        print("PIP did stop")
    }

    /// playerViewController failed to start PIP
    func playerViewController(playerViewController: AVPlayerViewController, failedToStartPictureInPictureWithError error: NSError) {
        print("PIP Error : \(error.localizedDescription)")
    }

    /// playerViewController automatically dismiss at PIP Start.
    func playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart(playerViewController: AVPlayerViewController) -> Bool {
        return false
    }

    /// playerViewController restore Interface For PIP
    func playerViewController(playerViewController: AVPlayerViewController, restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: (Bool) -> Void) {

    // if topView and navigation controller :
        if let topViewController = navigationController?.topViewController {
            topViewController.presentViewController(playerViewController, animated: true) {
                print("ready detailvc restore presentViewController")
                completionHandler(true)
            }
        }
        else {
            completionHandler(false)
        }
    }
}

Build and Run! Switch to Home or another App in your simulator or devices and test!

ios9-newapi-ipad-multitasking-pip-example's People

Contributors

sweefties avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

zeroizehd

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.