简体   繁体   English

iOS swift,视频输出不在 UIView 内居中

[英]iOS swift , video output not centering inside the UIView

So I got this video playing inside a sublayer of the view , but the video keeps popping underneath the UIView not inside of it any solution ?所以我让这个视频在视图的子层内播放,但视频一直在UIView下方弹出,而不是在它里面有什么解决方案吗?

import UIKit
import AVKit
import AVFoundation
import MediaPlayer

class ViewController: UIViewController {

    var player : AVPlayer? = nil
    var playerLayer : AVPlayerLayer? = nil
    var asset : AVAsset? = nil
    var playerItem: AVPlayerItem? = nil

    @IBOutlet weak var videoView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let videoURLWithPath = "http://selevision9890-i.akamaihd.net/hls/live/219323/98906/1.m3u8"
        let videoURL = NSURL(string: videoURLWithPath)
        asset = AVAsset(URL: videoURL!) as AVAsset
        playerItem = AVPlayerItem(asset: asset!)
        videoView.frame = self.view.frame
        player = AVPlayer(playerItem: self.playerItem!)
        playerLayer = AVPlayerLayer(player: self.player)
        playerLayer!.frame = videoView.frame
        videoView.layer.addSublayer(self.playerLayer!)

        player!.play()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

normal view:正常视图:

截屏

(from here: http://prnt.sc/e33dhf ) (从这里: http : //prnt.sc/e33dhf

rotated view:旋转视图:

截屏

(from here: http://prnt.sc/e33djs ) (从这里: http : //prnt.sc/e33djs

Move this code into viewDidAppear or viewWillAppear - in viewDidLoad the frames are not correct yet. 将此代码移到viewDidAppearviewWillAppear -在viewDidLoad ,框架尚不正确。 The view is resized to fit the screen size after viewDidLoad . viewDidLoad之后,将视图调整为适合屏幕尺寸。

i had to ditch avplayer and use avplayerviewcontroller and it worked fine thank you all 我不得不抛弃avplayer并使用avplayerviewcontroller,它很好用,谢谢大家

import UIKit
import AVFoundation
import AVKit

class ViewController: UIViewController {

    let avPlayerViewController = AVPlayerViewController()
    var avPlayer:AVPlayer?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let movieUrl:NSURL? = NSURL( string :"http://selevision9890-i.akamaihd.net/hls/live/219323/98906/1.m3u8")

        if let url = movieUrl{
            self.avPlayer = AVPlayer(URL : url )
            self.avPlayerViewController.player = self.avPlayer
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func play(sender: AnyObject) {
        self.presentViewController(self.avPlayerViewController, animated: true) { 
            self.avPlayerViewController.player?.play()
        }
    }
}

My solution is to have your view like a canvas and basically draw the AVPlayerViewController on top of it:我的解决方案是让您的视图像画布一样,基本上在其上绘制 AVPlayerViewController:

@IBOutlet weak var layerVideoView: UIImageView!
guard let url = Bundle.main.path(forResource: "santana", ofType: "mp4") else { return debugPrint("video not found") }
let path = URL(fileURLWithPath: url)

let player = AVPlayer(url: path)
let playerController = AVPlayerViewController()
playerController.player = player
playerController.view.frame = CGRect(x: layerVideoView.frame.origin.x, y: layerVideoView.frame.origin.y, width: layerVideoView.frame.width, height: layerVideoView.frame.height)
self.addChild(playerController)
self.view.addSubview(playerController.view)

LayerVideoView is an image view, but you can use just a UIView if that suits. LayerVideoView 是一个图像视图,但如果适合,您可以只使用 UIView。 Its is important to add constraints in the Storyboard to your View because your Video Player will mimic the shape of your View.将 Storyboard 中的约束添加到您的视图中很重要,因为您的视频播放器将模仿您的视图的形状。

Set PlayerLayer videogravity 设置PlayerLayer视频重力

let videoURLWithPath = "http://selevision9890-i.akamaihd.net/hls/live/219323/98906/1.m3u8"
let videoURL = NSURL(string: videoURLWithPath)
playerItem = AVPlayerItem(url: videoURL! as URL)
player = AVPlayer(playerItem: self.playerItem!)
playerLayer = AVPlayerLayer(player: self.player)
playerLayer.frame =  videoView.frame
playerLayer.backgroundColor = UIColor.black.cgColor
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
videoView.layer.addSublayer(self.playerLayer!)
player!.play()

I had similiar problem. 我有类似的问题。 I went with subclassing UIView like this: 我去像这样子类化UIView:

import UIKit

class VideoPlayerView: UIView {
var playerLayer: CALayer?

override func layoutSublayersOfLayer(layer: CALayer) {
    super.layoutSublayersOfLayer(layer)
    playerLayer?.frame = self.bounds
  }
}

and use it for setting up a video like this: 并用它来设置这样的视频:

@IBOutlet weak var videoPlayer: VideoPlayerView!

func setupVideoOnView() {
    var player = AVPlayer(URL: mediaURL! as NSURL)
    let playerLayer = AVPlayerLayer(player: player)

    playerLayer.frame = self.videoPlayer.bounds
    playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
    self.videoPlayer.layer.addSublayer(playerLayer)
    self.videoPlayer.playerLayer = playerLayer

}

That did a trick with wrong frame. 那是用错误的框架欺骗了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM