在使应用程序背景化时,AVPictureInPictureController不会自动启动画中画 [英] AVPictureInPictureController doesn't automatically start picture-in-picture when backgrounding the app

查看:126
本文介绍了在使应用程序背景化时,AVPictureInPictureController不会自动启动画中画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 AVPlayer + AVPlayerLayer + AVPictureInPictureController 为运行iOS 14(测试版7)的iPhone创建自定义视频播放器时,视频会执行在从UIButton动作调用 player.start()后,应用进入背景时,不会自动进入画中画模式.

When creating a custom video player using the AVPlayer + AVPlayerLayer + AVPictureInPictureController for a iPhone running iOS 14 (beta 7) the video does not automatically enter picture-in-picture-mode when the app enters the background after player.start() is called from a UIButton action.

该问题无法使用 AVPlayerViewController 重现,这似乎通常表明iOS 14上的 AVPictureInPictureController 有问题,但是我想知道是否还有其他人遇到了这个问题,并知道任何解决方法.我也已在 rdar://8620271

The issue does not reproduce using the AVPlayerViewController which seems to indicate a problem with the AVPictureInPictureController on iOS 14 in general, but I was wondering if anyone else had run into this problem and know of any workarounds. I've also filed this problem with Apple under rdar://8620271

示例代码.

import UIKit
import AVFoundation
import AVKit

class ViewController: UIViewController {
    private let player = AVPlayer(url: URL(string: "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4")!)
    private var pictureInPictureController: AVPictureInPictureController!
    private var playerView: PlayerView!
    private var playButton: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        playerView = PlayerView(frame: CGRect(x: 0, y: 44, width: view.bounds.width, height: 200))
        playerView.backgroundColor = .black
        playerView.playerLayer.player = player
        view.addSubview(playerView)

        playButton = UIButton(frame: CGRect(x: view.bounds.midX - 50, y: playerView.frame.maxY + 20, width: 100, height: 22))
        playButton.setTitleColor(.blue, for: .normal)
        playButton.setTitle("Play", for: .normal)
        playButton.addTarget(self, action: #selector(play), for: .touchUpInside)
        view.addSubview(playButton)

        pictureInPictureController = AVPictureInPictureController(playerLayer: playerView.playerLayer)

        do {
            let audioSession = AVAudioSession.sharedInstance()
            try audioSession.setCategory(.playback)
            try audioSession.setMode(.moviePlayback)
            try audioSession.setActive(true)
        } catch let e {
            print(e.localizedDescription)
        }
    }

    @objc func play() {
        player.play()
    }
}

class PlayerView: UIView {
    override class var layerClass: AnyClass {
        return AVPlayerLayer.self
    }

    var playerLayer: AVPlayerLayer! {
        return layer as? AVPlayerLayer
    }
}

推荐答案

问题的根本原因是双重的:

The root cause of the problem ended up being twofold:

  1. AVAudioSession.sharedInstance().setActive(true)必须在初始化 AVPictureInPictureController 之前被称为.

  1. AVAudioSession.sharedInstance().setActive(true) must be called before the AVPictureInPictureController is initialised.

AVPlayerLayer 的帧大小必须具有不大于16/9的宽高比(作为单独的bug提交, rdar//8689203 )

The frame size for the AVPlayerLayer must have a aspect ratio no greater than 16/9 (filed as a separate bug, rdar//8689203)

对于iPad,视频的宽度必须与设备相同(在任何给定的方向上).没有单独的雷达,因为苹果已经承认了另一个错误.

For iPads, the video must be the same width as the device (in any given orientation). No separate rdar, as Apple have acknowledged the other bug already.

(上面的示例中没有第二个问题)

(The 2nd issues is not present in the example above)

Apple已经确认了这些错误,并向我报告说,这些错误已经/将得到修复(一种罕见的雷达实际导致回复的情况!)

Apple have acknowledged these bugs, and reported back to me that they have been / will be fixed (a rare case of a radar actually resulting in a reply!)

这篇关于在使应用程序背景化时,AVPictureInPictureController不会自动启动画中画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆