我想在单元格中使用 AVPlayerController 的实例。 布局是collectionView。
播放器可以很好地处理包括约束在内的第一个视频。 第二次,约束无法实现。更糟糕的是,播放器没有播放。
有人会对我在这里想念的东西有任何想法吗?
这是下面的演示:
class MediaPlayerCell: BaseCell {
let statusBarHeight: CGFloat = 20
let contentOffset: CGFloat = 50
static let vgReuseIdentifier = "VGMediaPlayerCell"
var toggleHeaderVisibility: Bool = false
static var playerIsPlaying: Bool = false
public weak var delegate: VGMediaPlayerCellDelegate?
var waitingIndicator = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.whiteLarge)
var containerView = UIView()
var messageLabel = UILabel()
var content: VGContent?
let deviceOrientation = UIDevice.current.orientation
//player settings
var player: AVPlayer?
var asset: AVAsset?
var isReadyForDisplayObserver: NSKeyValueObservation?
var avPlayerViewConroller = AVPlayerViewController()
var needAutoPlay: Bool = false
var isLoaded: Bool = false
func configurePlayer(with viewModel: VGMediaPlayerViewModel) {
//hideErrorMessage()
//avPlayerViewConroller = AVPlayerViewController()
avPlayerViewConroller.videoGravity = AVLayerVideoGravity.resizeAspect
insertSubview(avPlayerViewConroller.view, at: 0)
avPlayerViewConroller.view.topAnchor.constraint(equalTo: topAnchor).isActive = true
avPlayerViewConroller.view.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
avPlayerViewConroller.view.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
avPlayerViewConroller.view.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
self.bringSubviewToFront(avPlayerViewConroller.view)
updateUI(with: viewModel)
guard let url = viewModel.url! as? URL else { return }
asset = AVAsset(url: url)
guard let asset = asset else {
waitingStateActive(isActive: false)
//showErrorMessage("La prévisualisation n'est pas disponible pour ce fichier")
return
}
if !asset.isPlayable {
DispatchQueue.main.async {
self.waitingStateActive(isActive: false)
//self.showErrorMessage("La prévisualisation n'est pas disponible pour ce fichier")
return
}
}
let item = AVPlayerItem(asset: asset)
self.player = AVPlayer(playerItem: item)
if #available(iOS 10.0, *) {
self.player?.automaticallyWaitsToMinimizeStalling = false
}
DispatchQueue.main.async {
self.avPlayerViewConroller.player = self.player
if self.needAutoPlay {
self.player?.play()
}
if self.content?.type == .audio && !asset.isPlayable {
self.waitingStateActive(isActive: false)
}
self.isLoaded = true
}
}
func setupConstraints() {
//custom insets per device orientation
// regular from for iphone 8 and downwards
// custom frame for iphone X and upwards
if UIDevice().userInterfaceIdiom == .phone {
switch UIScreen.main.nativeBounds.height {
//iPhone 5 or 5S or 5C, iPhone 6/6S/7/8, iPhone 6+/6S+/7+/8+
case 1136, 1334, 1920, 2208:
avPlayerViewConroller.view.frame = self.frame
//iPhone X, Xs, iPhone Xs Max, iPhone Xr
case 2436, 2688, 1792:
if UIApplication.shared.statusBarOrientation.isPortrait {
avPlayerViewConroller.view.frame = self.frame.insetBy(dx: 0.0, dy: 60.0)
} else {
avPlayerViewConroller.view.frame = self.frame.insetBy(dx: 30.0, dy: 20.0)
}
default: break
}
} else {
//for the iPad
avPlayerViewConroller.view.frame = self.frame
}
//Add observer on keypath rate to monitor player's playing status
if self.toggleHeaderVisibility == true {
if UIDevice().userInterfaceIdiom == .phone {
switch UIScreen.main.nativeBounds.height {
case 2436, 2688, 1792:
player?.addObserver(self, forKeyPath: "rate", options: [.old, .new], context: nil)
default : break
}
}
}
//player?.addObserver(self, forKeyPath: "rate", options: [.old, .new], context: nil)
self.translatesAutoresizingMaskIntoConstraints = true
setNeedsLayout()
}
}
感谢您的帮助!