gpt4 book ai didi

ios - 在 Swift 3 中创建具有自定义大小的 VideoPlayer

转载 作者:行者123 更新时间:2023-11-28 18:49:36 26 4
gpt4 key购买 nike

我想以自定义大小显示 .mov 文件。我的 Controller 有一个 xib 文件。

enter image description here

顶 View 是 UIView 的子类。我想在此 View 中显示视频。有我创建视频 View 的代码。但是我的视频播放器的大小有问题。如何解决这个问题。

这是我的代码:

 private func playVideo() {
guard let path = Bundle.main.path(forResource: "video_2017-06-30_15-49-05", ofType:"mov") else {
debugPrint("video.m4v not found")
return
}
let player = AVPlayer(url: URL(fileURLWithPath: path))
let playerController = AVPlayerViewController()
playerController.player = player

let avPlayerLayer = AVPlayerLayer(player: player)
avPlayerLayer.frame = videoView.layer.frame
videoView.layer.insertSublayer(avPlayerLayer, at: 0)


}

最佳答案

如果您希望使用搜索栏和播放暂停按钮将本地播放器添加到您的 View 中,请使用以下代码:

private func playVideo() {
guard let path = Bundle.main.path(forResource: "SampleVideo", ofType:"mp4") else {
debugPrint("video.m4v not found")
return
}
let player = AVPlayer(url: URL(fileURLWithPath: path))
let playerController = AVPlayerViewController()
playerController.player = player
playerController.view.frame = videoView.bounds
videoView.addSubview(playerController.view)
player.play()
}

如果您只想使用 AVPlayerLayer 在您的 View 中播放视频,那么您可以执行以下操作:

    var player : AVPlayer!
var avPlayerLayer : AVPlayerLayer!
@IBOutlet weak var videoView: UIView!

override func viewDidLoad() {
super.viewDidLoad()


guard let path = Bundle.main.path(forResource: "SampleVideo", ofType:"mp4") else {
debugPrint("video.m4v not found")
return
}
player = AVPlayer(url: URL(fileURLWithPath: path))
avPlayerLayer = AVPlayerLayer(player: player)
avPlayerLayer.videoGravity = AVLayerVideoGravity.resize

videoView.layer.addSublayer(avPlayerLayer)
playVideo()


}

override func viewDidLayoutSubviews() {

avPlayerLayer.frame = videoView.layer.bounds
}

private func playVideo() {
player.play()

}

这对我来说很完美,希望它对你有用:)

关于ios - 在 Swift 3 中创建具有自定义大小的 VideoPlayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44846098/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com