首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVPlayer去掉顶部和底部的黑条/背景(Swift)

AVPlayer去掉顶部和底部的黑条/背景(Swift)
EN

Stack Overflow用户
提问于 2018-01-27 01:11:56
回答 2查看 3.2K关注 0票数 5

快速问一下,有谁知道怎样才能去掉视频顶部和底部的黑条?我刚刚开始使用AVPlayer,我只是在这里和那里删除代码,试图删除黑色图层。感谢那些能帮助我的人,谢谢!

UIViewController

代码语言:javascript
复制
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()
    }

}

EN

回答 2

Stack Overflow用户

发布于 2018-02-01 18:22:51

您需要设置AVLayer的videoGravity。默认值为.resizeAspect,它将在顶部/底部或左侧/右侧保留黑条的纵横比。你想要的是.resize

需要在其上设置videoGravity的层属性是AVPlayer的AVLayer属性。

有关更多信息,请参阅:https://developer.apple.com/documentation/avfoundation/avplayerlayer/1388915-videogravity

票数 6
EN

Stack Overflow用户

发布于 2020-07-30 17:36:12

代码语言:javascript
复制
#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。它必须设置为超级视图的背景颜色。

代码语言:javascript
复制
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.
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48466254

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档