iOS音视频开发 PictureInPicture

Picture in Picture (PiP)在是iOS9新添加的功能,允许iPad用户在悬浮小窗口播放视频。效果如下

Figure 4-1  PiP in Action

 你可以使用AVKit framework的AVPlayerViewController类来实现,或者如果自定义了播放器,使用AVPictureInPictureController来实现。在使用AVPlayerViewController来播放视频时,当你设置AVAudioSession的category为AVAudioSessionCategoryPlayback时,系统默认就支持这种功能。当用户点击最左边的按钮,退出这种播放模式时,播放界面会恢复到正常情况,但是播放会停止,你需要实现AVPlayerViewControllerDelegate代理,来处理这种情况

func playerViewController(_ playerViewController: AVPlayerViewController,
                          restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: (Bool) -> Void) {
    present(playerViewController, animated: true) {
        completionHandler(false)
    }
}

在自己写的播放器里实现PiP效果需要自定义UI界面,需要检测系统是否自持这种播放效果

func setupPictureInPicture() {
    // Ensure PiP is supported by current device
    if AVPictureInPictureController.isPictureInPictureSupported() {
        // Create new controller passing reference to the AVPlayerLayer
        pictureInPictureController = AVPictureInPictureController(playerLayer: playerLayer)
        pictureInPictureController.delegate = self
        let keyPath = #keyPath(AVPictureInPictureController.isPictureInPicturePossible)
        pictureInPictureController.addObserver(self,
                                               forKeyPath: keyPath,
                                               options: [.initial, .new],
                                               context: &pictureInPictureControllerContext)
    } else {
        // PiP not supported by current device. Disable PiP button.
        pictureInPictureButton.isEnabled = false
    }
}

使用pictureInPictureController.stopPictureInPicture()和pictureInPictureController.startPictureInPicture()方法来实现开始和关闭PiP播放模式,通过以下代理方法监听,生命周期事件

func pictureInPictureControllerWillStartPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
    // hide playback controls
    // show placeholder artwork
}
 
func pictureInPictureControllerDidStopPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
    // hide placeholder artwork
    // show playback controls
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值