我有一个基于VideoPlayer类的UIView,当我打开视频文件时,它不“适合” UIView的边界。这会导致顶部和底部位于UIView之外,并且看不到播放控件。
我尝试将“内容模式”设置为“缩放以适合”,“长宽比适合”,“长宽比填充”等,但没有任何作用。
如何使其“填充” UIView?
PlayerViewClass:
import AVFoundation
import AVKit
class PlayerViewClass: UIView {
override static var layerClass: AnyClass{
return AVPlayerLayer.self;
}
var playerLayer: AVPlayerLayer{
return layer as! AVPlayerLayer
}
var player: AVPlayer?{
get{
return playerLayer.player
}
set{
playerLayer.player = newValue
}
}
}
在我的主要ViewController.swift类中:
@IBOutlet weak var cameraView: PlayerViewlass!
func getCacheDirectory () -> URL {
// return the documents url
let fm = FileManager.default
let docsUrl = try! fm.url(for:.documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
return docsUrl
}
func getFileURL() -> URL{
// Get url of the file
let path = getCacheDirectory()
let filePath = path.appendingPathComponent("\(videoFileName)")
return filePath
}
@IBAction func playVideoFile(_ sender: Any) {
let path = getFileURL()
let player = AVPlayer(url: path)
cameraView.playerLayer.player = player
cameraView.player?.play()
}