[英]My video goes off the screen, but stays within it's UIView
我有一個長寬比為 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()
}
}
你需要把線放在里面
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()
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.