簡體   English   中英

AVPlayer 去除頂部和底部的黑條/背景(Swift)

[英]AVPlayer get rid of black bars/background at top & bottom (Swift)

快速提問,有人知道如何去除視頻頂部和底部的黑條嗎? 我剛開始使用 AVPlayer,我只是在這里和那里刪除代碼以嘗試刪除黑色圖層。 感謝那些可以幫助我的人,謝謝!

視圖控制器

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()
    }

}

在此處輸入圖片說明

您需要設置 AVLayer 的 videoGravity。 默認值為.resizeAspect ,它將保留頂部/底部或左側/右側帶有黑條的縱橫比。 你想要的是.resize

您需要設置 videoGravity 的 AVLayer 是您的 AVPlayer 的圖層屬性。

有關更多信息,請參閱: https : //developer.apple.com/documentation/avfoundation/avplayerlayer/1388915-videogravity

#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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM