使用iOS swift时,AVPlayer在横向模式下未全屏显示

7vux5j2d  于 2023-02-14  发布在  iOS
关注(0)|答案(3)|浏览(222)

我有一个视频视图设置为全屏。但是,当在模拟器中播放时,它没有运行全屏。
此问题仅适用于iPad,不适用于iPhone。
下面是我的代码:

override func viewDidAppear(_ animated: Bool) {

    super.viewDidAppear(true)

    let bundle: Bundle = Bundle.main
    let videoPlayer: String = bundle.path(forResource: "normalnewer", ofType: "mp4")!
    let movieUrl : NSURL = NSURL.fileURL(withPath: videoPlayer) as NSURL
    //        var fileURL = NSURL(fileURLWithPath: "/Users/Mantas/Desktop/123/123/video-1453562323.mp4.mp4")
    playerView = AVPlayer(url: movieUrl as URL)

    NotificationCenter.default.addObserver(self,selector: #selector(playerItemDidReachEnd),name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,object: self.playerView.currentItem) // Add observer

            var playerLayer=AVPlayerLayer(player: playerView)
//        self.avPlayerLayer.playerLayer = AVLayerVideoGravityResizeAspectFill

    playerLayer.frame = viewVideo.bounds
//        self.playerLayer.frame = self.videoPreviewLayer.bounds

    viewVideo.layer.addSublayer(playerLayer)



    playerView.play()

    UserDefaults.standard.set("normalnewer", forKey: "video")



}

我试过检查目标的一般横向模式。通过下面的线程:how to play a video in fullscreen mode using swift ios?
Play video fullscreen in landscape mode, when my entire application is in lock in portrait mode
Setting device orientation in Swift iOS
但是这些并没有解决我的问题。这里是一个截图。

有人能帮我解决这个问题吗?

xqk2d5yq

xqk2d5yq1#

我看到你解决了你的问题,但我注意到你使用了AVPlayerLayer。方向旋转是用AVPlayerViewController处理的,但不是用播放器层的自定义视图。能够在不旋转设备的情况下将层置于全屏是有用的。我在别处回答了这个问题,但我也会在这里提出。
变换和帧操纵可以解决此问题:

extension CGAffineTransform {

    static let ninetyDegreeRotation = CGAffineTransform(rotationAngle: CGFloat(M_PI / 2))
}

extension AVPlayerLayer {

    var fullScreenAnimationDuration: TimeInterval {
        return 0.15
    }

    func minimizeToFrame(_ frame: CGRect) {
        UIView.animate(withDuration: fullScreenAnimationDuration) {
            self.setAffineTransform(.identity)
            self.frame = frame
        }
    }

    func goFullscreen() {
        UIView.animate(withDuration: fullScreenAnimationDuration) {
            self.setAffineTransform(.ninetyDegreeRotation)
            self.frame = UIScreen.main.bounds
        }
    }
}

设置AVPlayerLayer的帧会改变它的父帧。将原始帧保存在视图子类中,以将AVPLayer最小化回原来的位置。这允许自动布局。
重要提示--只有当播放器在视图子类的中心时才有效。
不完整示例:

class AVPlayerView: UIView {

    fileprivate var avPlayerLayer: AVPlayerLayer {
        return layer as! AVPlayerLayer
    }

    fileprivate var hasGoneFullScreen = false
    fileprivate var isPlaying = false
    fileprivate var originalFrame = CGRect.zero

    func togglePlayback() {
        if !hasGoneFullScreen {
            originalFrame = frame
            hasGoneFullScreen = true
        }

        isPlaying = !isPlaying
        if isPlaying {
            avPlayerLayer.goFullscreen()
            avPlayerLayer.player?.play()
        } else {
            avPlayerLayer.player?.pause()
            avPlayerLayer.minimizeToFrame(originalFrame)
        }
    }
}
jdzmm42g

jdzmm42g2#

在花了一些时间仔细看了故事板之后,我试着把填充改成Aspect Fill & Fit,这样就解决了问题:)

gj3fmq9x

gj3fmq9x3#

你能做的最好的事情就是把这段代码放到viewDidLayoutSubviews中并享受它!

DispatchQueue.main.async {
        self.playerLayer?.frame = self.videoPlayerView.bounds
    }

相关问题