快速问一下,有谁知道怎样才能去掉视频顶部和底部的黑条?我刚刚开始使用AVPlayer,我只是在这里和那里删除代码,试图删除黑色图层。感谢那些能帮助我的人,谢谢!
UIViewController
import UIKit
import AVKit
import AVFoundation
private var looper: AVPlayerLooper?
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let path = Bundle.main.path(forResource: "Innie-Kiss", ofType:"mp4")
let player = AVQueuePlayer(url: URL(fileURLWithPath: path!))
looper = AVPlayerLooper(player: player, templateItem: AVPlayerItem(asset: AVAsset(url: URL(fileURLWithPath: path!))))
let controller = AVPlayerViewController()
controller.player = player
let screenSize = UIScreen.main.bounds.size
let videoFrame = CGRect(x: 0, y: 200, width: screenSize.width, height: (screenSize.height - 130) / 2)
controller.view.frame = videoFrame
self.view.addSubview(controller.view)
player.play()
}
}

发布于 2018-02-01 18:22:51
您需要设置AVLayer的videoGravity。默认值为.resizeAspect,它将在顶部/底部或左侧/右侧保留黑条的纵横比。你想要的是.resize
需要在其上设置videoGravity的层属性是AVPlayer的AVLayer属性。
有关更多信息,请参阅:https://developer.apple.com/documentation/avfoundation/avplayerlayer/1388915-videogravity
发布于 2020-07-30 17:36:12
#import AVKit
var playerController = AVPlayerViewController()
playerController.videoGravity = .resizeAspect
/*playerController.videoGravity = .resizeAspectFill*/
playerController.view.backgroundColor = UIColor.white //set color as per super or custom view.
Note: If you set the videoGravity of the AVPlayerViewController. The default value is .resizeAspect then it is possible to visibility of black color(default) in background if you wants this color would similar to your super view's color then you must set superview's or custom view's color to your playercontroller's view background color.*示例:假设超级视图或自定义视图的背景色为白色,则playerController视图的背景色必须设置为playerController.view.backgroundColor = UIColor.white *
注意:playerController的backgroundColor不应该总是设置为UIColor.white。它必须设置为超级视图的背景颜色。
If you set playerController.videoGravity = .resizeAspectFill then it will fill the player content to super view or custom view, here also no black color would be shown. So choose either .resizeAspect or resizeAspectFill as per your requirements.https://stackoverflow.com/questions/48466254
复制相似问题