简体   繁体   中英

My video goes off the screen, but stays within it's UIView

I have a UIView that has an aspect ratio of 16:9 with constraints set up accordingly. However, the video stays within the UIView but half of it goes off the screen. Anyone know how to fix this? Just to clarify I am trying to create a video inside a 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()
}

}

You need to put the line inside

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

Also inside viewDidLoad set

self.videoView.clipsToBounds = true

and declare

var playerLayer:AVPlayerLayer!

then

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()
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM