如何隐藏AVPictureInPictureController的默认关闭按钮



使用AVPictureInPictureController实现pip函数。我隐藏了播放器和时间搜索,但未能隐藏最后一个关闭按钮和返回按钮。

我想隐藏的按钮就是这些按钮。在此处输入图像描述

我引用的是下面的github。

https://github.com/uakihir0/UIPiPDemo

我用来声明AVPictureInPictureController的来源如下。

import UIKit
import AVKit
class ViewController: UIViewController {
@IBOutlet weak var labelTest: UILabel!
private var pipUi = PipUi()
private var _pipController: AVPictureInPictureController?
private let _bufferDisplayLayer = AVSampleBufferDisplayLayer()
private var _pipPossibleObservation: NSKeyValueObservation?
private var _observer: NSObjectProtocol?
override func viewDidLoad() {
super.viewDidLoad()

let margin = ((view.bounds.width - 200) / 2)
let videoContainerView = UIView()
videoContainerView.frame = .init(x: margin, y: 200, width: 200, height: 30)
videoContainerView.backgroundColor = UIColor.blue
self.view.addSubview(videoContainerView)
let bufferDisplayLayer = pipUi.bufferDisplayLayer
bufferDisplayLayer.frame = videoContainerView.bounds
bufferDisplayLayer.backgroundColor = UIColor.brown.cgColor
bufferDisplayLayer.videoGravity = .resizeAspect
videoContainerView.layer.addSublayer(bufferDisplayLayer)

_bufferDisplayLayer.flush()
pipUi.start()

labelTest.setOnTouchListener(self, action: #selector(toggle))
DispatchQueue.main.async {
self.start()
}
}
func start() {

// PinP をサポートしているデバイスかどうかを確認
if AVPictureInPictureController.isPictureInPictureSupported() {

// AVPictureInPictureController の生成
_pipController = AVPictureInPictureController(
contentSource: .init(
sampleBufferDisplayLayer:
pipUi.bufferDisplayLayer,
playbackDelegate: self))
_pipController?.delegate = self

_pipPossibleObservation = _pipController?.observe(
AVPictureInPictureController.isPictureInPicturePossible,
options: [.initial, .new],
changeHandler: { [weak self] _, changed in
DispatchQueue.main.async {
if changed.newValue == true {
//self?.start()
}
}
})
}

if #available(iOS 14.0, *) {
//Hide the seekbar
_pipController?.requiresLinearPlayback = true
} else {
// Fallback on earlier versions
}

//Hide the play/pause controls
_pipController?.setValue(true, forKey: "controlsStyle")

}
@objc func toggle() {
guard let _pipController = _pipController else { return }
if !_pipController.isPictureInPictureActive {
print("start")
_pipController.startPictureInPicture()
} else {
print("stop")
_pipController.stopPictureInPicture()
}
}
}

extension ViewController: AVPictureInPictureControllerDelegate {
func pictureInPictureController(
_ pictureInPictureController: AVPictureInPictureController,
failedToStartPictureInPictureWithError error: Error
) {
print("(#function)")
print("pip error: (error)")
}
func pictureInPictureControllerWillStartPictureInPicture(
_ pictureInPictureController: AVPictureInPictureController
) {
print("(#function)")
}
func pictureInPictureControllerWillStopPictureInPicture(
_ pictureInPictureController: AVPictureInPictureController
) {
print("(#function)")
}
}
extension ViewController: AVPictureInPictureSampleBufferPlaybackDelegate {
func pictureInPictureController(
_ pictureInPictureController: AVPictureInPictureController,
setPlaying playing: Bool
) {
print("(#function)")
}
func pictureInPictureControllerTimeRangeForPlayback(
_ pictureInPictureController: AVPictureInPictureController
) -> CMTimeRange {
print("(#function)")
return CMTimeRange(start: .negativeInfinity, duration: .positiveInfinity)
}
func pictureInPictureControllerIsPlaybackPaused(
_ pictureInPictureController: AVPictureInPictureController
) -> Bool {
print("(#function)")
return false
}
func pictureInPictureController(
_ pictureInPictureController: AVPictureInPictureController,
didTransitionToRenderSize newRenderSize: CMVideoDimensions
) {
print("(#function)")
print(newRenderSize)
}
func pictureInPictureController(
_ pictureInPictureController: AVPictureInPictureController,
skipByInterval skipInterval: CMTime,
completion completionHandler: @escaping () -> Void
) {
print("(#function)")
completionHandler()
}

}

我想隐藏关闭按钮以自定义它。请帮助。

我相信你可以完全隐藏"关闭";以及";返回应用程序";按钮,因为它们对于PiP的使用是必不可少的,并且没有API来隐藏它们(据我所知(。然而,如果你点击视频屏幕,所有按钮都会暂时隐藏。播放2秒后,这些按钮也会自动隐藏。此外,苹果在以下支持指南中谈到了这些按钮作为与PiP交互的手段

相关内容

  • 没有找到相关文章

最新更新