【问题标题】:My video goes off the screen, but stays within it's UIView我的视频离开屏幕,但留在它的 UIView
【发布时间】:2019-06-20 22:26:05
【问题描述】:

我有一个纵横比为 16:9 的 UIView,并相应地设置了约束。但是,视频保留在 UIView 中,但有一半离开了屏幕。有人知道怎么修这个东西吗?只是为了澄清我正在尝试在 UIView 中创建视频。

import Foundation
import UIKit
import AVKit
import AVFoundation

class WallPush: UIViewController {

@IBOutlet weak var videoView: UIView!
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var textView: UILabel!

var player: AVPlayer?

override func viewDidLoad() {
    super.viewDidLoad()

    // Load video resource
    if let videoUrl = Bundle.main.url(forResource: "WallPush", withExtension: "mp4") {

        if #available(iOS 10.0, *) {
            player?.playImmediately(atRate: 1.0)
        } else {
            // Fallback on earlier versions
        }
        // Init video
        self.player = AVPlayer(url: videoUrl)
        self.player?.isMuted = true
        self.player?.actionAtItemEnd = .none

        // Add player layer
        let playerLayer = AVPlayerLayer(player: player)
        playerLayer.videoGravity = AVLayerVideoGravity.resizeAspect
        playerLayer.frame = videoView.bounds

        // Add video layer
        self.videoView.layer.addSublayer(playerLayer)

        // Play video
        self.player?.play()

        // Observe end
        NotificationCenter.default.addObserver(self, selector: #selector(playerItemDidReachEnd), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem)
    }
}


// MARK: - Loop video when ended.
@objc func playerItemDidReachEnd(notification: NSNotification) {
    self.player?.seek(to: CMTime.zero)
    self.player?.play()
}

}

【问题讨论】:

    标签: ios swift video


    【解决方案1】:

    你需要把线放在里面

    override func viewDidLayoutSubviews() {
       super.viewDidLayoutSubviews()
       playerLayer.frame = videoView.bounds
    }
    

    也在viewDidLoad集合内

    self.videoView.clipsToBounds = true
    

    并声明

    var playerLayer:AVPlayerLayer!
    

    然后

    playerLayer = AVPlayerLayer(player: player)
    playerLayer.videoGravity = AVLayerVideoGravity.resizeAspect
    

     class WallPush: UIViewController {
    
        @IBOutlet weak var videoView: UIView!
        @IBOutlet weak var scrollView: UIScrollView!
        @IBOutlet weak var textView: UILabel!
    
        var player: AVPlayer? 
    
        var playerLayer:AVPlayerLayer! 
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Load video resource
            if let videoUrl = Bundle.main.url(forResource: "WallPush", withExtension: "mp4") {
                print("File exists")
                if #available(iOS 10.0, *) {
                    player?.playImmediately(atRate: 1.0)
                } else {
                    // Fallback on earlier versions
                }
                // Init video
                self.player = AVPlayer(url: videoUrl)
                self.player?.isMuted = true
                self.player?.actionAtItemEnd = .none
                videoView.clipsToBounds = true
                // Add player layer
                playerLayer = AVPlayerLayer(player: player)
                playerLayer.videoGravity = AVLayerVideoGravity.resizeAspect
                playerLayer.frame = videoView.bounds  // you can comment it 
    
                // Add video layer
                self.videoView.layer.addSublayer(playerLayer)
    
                // Play video
                self.player?.play()
    
                // Observe end
                NotificationCenter.default.addObserver(self, selector: #selector(playerItemDidReachEnd), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem)
            }
            else {
                print("File not exists")
            }
        }
    
        override func viewDidLayoutSubviews() {
          super.viewDidLayoutSubviews()
          playerLayer.frame = videoView.bounds
        }
    
        // MARK: - Loop video when ended.
        @objc func playerItemDidReachEnd(notification: NSNotification) {
            self.player?.seek(to: CMTime.zero)
            self.player?.play()
        }
    
    }
    

    【讨论】:

    • 我要把它放在 viewDidLoad 函数下面吗?我还要从 viewDidLoad 中删除 playerLayer.frame = videoView.bounds 吗?
    • 是的,把它作为一个函数放在类中,不会有什么不同,因为框架无论如何都会被覆盖
    • 当我将代码放在 viewDidLoad 部分下方时,它显示“方法不会覆盖其超类中的方法”
    • 应用程序崩溃并在“playerLayer.frame = videoView.bounds”行显示“unexpectedly found nil”
    • 知道了!我不小心拼错了视频名称。您的所有建议都解决了整个问题。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-18
    • 2011-09-14
    • 1970-01-01
    相关资源
    最近更新 更多