Swift AV 播放器视频应为 AspectFill 或全屏
Swift AV Player Video Should be AspectFill OR Full Screen
在登录屏幕上,我必须以全分辨率显示背景视频。我可以通过在黑色区域双击下面的图像来做到这一点。
这是我要求的结果。但这不是通过编码完成的。我只是通过双击黑色区域来做到这一点。
这是我的代码 class。
class loginViewController: UIViewController {
@IBOutlet weak var videoView: UIView!
let player = AVPlayer()
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.isNavigationBarHidden = true
let myFileManager = FileManager.default
let mainBundle = Bundle.main
let resourcesPath = mainBundle.resourcePath!
guard let allItemsInTheBundle = try? myFileManager.contentsOfDirectory(atPath: resourcesPath) else {
return
}
let videoName = "login_video"
let videoPath = Bundle.main.path(forResource: videoName, ofType: "mp4")
let videoUrl = URL(fileURLWithPath: videoPath!)
let player = AVPlayer(url: videoUrl)
let controller=AVPlayerViewController()
controller.player=player
let playerLayer = AVPlayerLayer(player: self.player)
playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill;
controller.showsPlaybackControls = false
controller.view.frame = self.view.frame
videoView.addSubview(controller.view)
self.addChildViewController(controller)
player.play()
player.actionAtItemEnd = .none
loopVideo(player: player)
}
func loopVideo(player: AVPlayer) {
NotificationCenter.default.addObserver(forName: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil, queue: nil) { notification in
player.seek(to: kCMTimeZero)
player.play()
}
}
}
任何人都可以建议我怎样才能达到这个要求的结果。我找到了这个解决方案,但一脉相承。
let playerLayer = AVPlayerLayer(player: self.player)
playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill;
请建议我更好的方法。
提前致谢。
这段代码什么都不做:
let playerLayer = AVPlayerLayer(player: self.player)
playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill;
所做的只是创建一个新的播放器层并将其丢弃。
AVPlayerViewController已经有播放器层了!设置AVPlayerViewController的视频重力。
controller.videoGravity = ...
在登录屏幕上,我必须以全分辨率显示背景视频。我可以通过在黑色区域双击下面的图像来做到这一点。
这是我要求的结果。但这不是通过编码完成的。我只是通过双击黑色区域来做到这一点。
这是我的代码 class。
class loginViewController: UIViewController {
@IBOutlet weak var videoView: UIView!
let player = AVPlayer()
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.isNavigationBarHidden = true
let myFileManager = FileManager.default
let mainBundle = Bundle.main
let resourcesPath = mainBundle.resourcePath!
guard let allItemsInTheBundle = try? myFileManager.contentsOfDirectory(atPath: resourcesPath) else {
return
}
let videoName = "login_video"
let videoPath = Bundle.main.path(forResource: videoName, ofType: "mp4")
let videoUrl = URL(fileURLWithPath: videoPath!)
let player = AVPlayer(url: videoUrl)
let controller=AVPlayerViewController()
controller.player=player
let playerLayer = AVPlayerLayer(player: self.player)
playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill;
controller.showsPlaybackControls = false
controller.view.frame = self.view.frame
videoView.addSubview(controller.view)
self.addChildViewController(controller)
player.play()
player.actionAtItemEnd = .none
loopVideo(player: player)
}
func loopVideo(player: AVPlayer) {
NotificationCenter.default.addObserver(forName: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil, queue: nil) { notification in
player.seek(to: kCMTimeZero)
player.play()
}
}
}
任何人都可以建议我怎样才能达到这个要求的结果。我找到了这个解决方案,但一脉相承。
let playerLayer = AVPlayerLayer(player: self.player)
playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill;
请建议我更好的方法。 提前致谢。
这段代码什么都不做:
let playerLayer = AVPlayerLayer(player: self.player)
playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill;
所做的只是创建一个新的播放器层并将其丢弃。
AVPlayerViewController已经有播放器层了!设置AVPlayerViewController的视频重力。
controller.videoGravity = ...