ios - 我的视频离开屏幕,但留在它的 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()
}
}
推荐阅读
- mysql - 按列总和限制
- ios - 获取发送到另一个类的类和函数的名称
- chisel - 使用chisel实现一个比较大的项目,done 阐述的进度如何查看
- javascript - 为什么 couchDB 在尝试创建视图时给我未授权错误
- ms-wopi - 是否可以为 Office Online Server 指定默认 WOPI 主机?
- lua - 为什么我用 lua 协程实现的生产者-消费者模型无法正常工作?
- javascript - TypeScript:如何将函数添加为参数以及使用 React 函数组件进行道具解构?
- nonlinear-optimization - 为什么这个 GEKKO 脚本没有产生更好的解决方案?
- r - 不读取向量的第二个元素
- php - PHP 中的 JSON API 请求无法正常工作