1

resizeAspect 因为视频重力仅适用于我,在使用 iPhone X 时。

由于某些原因,黑色纵横比只会添加到顶部而不是底部。这是我不使用 iPhone X 时的样子(图片为白色)

苹果手机

它应该是这样的:

正常状态

如您所见,在 iPhone X 上,一切看起来都像预期的那样干净和平衡。

这是我播放视频的方式:

    avPlayerLayer = AVPlayerLayer(player: avPlayer)
    avPlayerLayer.frame = PreviewLayer.bounds
    avPlayerLayer.videoGravity = .resizeAspect //Will automatically add black bars


    PreviewLayer.layer.insertSublayer(avPlayerLayer, at: 0)
    let playerItem = AVPlayerItem(url: video)
    avPlayer?.replaceCurrentItem(with: playerItem)
    avPlayer?.play()
4

2 回答 2

1

试试这个步骤

1-检查底部约束是否与安全区连接

在此处输入图像描述

2-试试这个代码

import UIKit
import AVKit

class ViewController: UIViewController {
    @IBOutlet weak var playerView: UIView!
    private var player: AVPlayer!

    override func viewDidLoad() {
        super.viewDidLoad()
       playVideo(from: "Intro.mp4")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    private func playVideo(from file:String) {
        let file = file.components(separatedBy: ".")

        guard let path = Bundle.main.path(forResource: file[0], ofType:file[1]) else {
            debugPrint( "\(file.joined(separator: ".")) not found")
            return
        }

        player = AVPlayer(url: URL(fileURLWithPath: path))

        NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.player.currentItem, queue: .main) { _ in
            self.player?.seek(to: kCMTimeZero)
            self.player?.play()
        }

        let playerLayer = AVPlayerLayer(player: player)
        playerLayer.videoGravity = AVLayerVideoGravity.resizeAspect
        playerLayer.frame = playerView.layer.bounds
        playerView.layer.insertSublayer(playerLayer, at: 0)
        player.play()
    }

}

iphone X 上的结果

在此处输入图像描述

iphone 7 上的结果

在此处输入图像描述

注意:如果您需要所有设备上的视频完整视图,请尝试链接Bottom constraintTop constraint使用超级视图而不是安全区域

于 2018-04-15T20:00:41.680 回答
0

使您的 videoLayer 视图成为以下子视图:

class VideoContainerView: UIView {

    var playerLayer: CALayer?

    override func layoutSublayers(of layer: CALayer) {
        super.layoutSublayers(of: layer)
        playerLayer?.frame = self.bounds
    }

}

并添加:

self.VideoShareLayer.playerLayer = avPlayerLayer;

在插入图层之前。

于 2018-04-16T11:44:55.763 回答