swift 如何在UIView中播放视频?

mw3dktmi  于 2023-04-28  发布在  Swift
关注(0)|答案(5)|浏览(242)

我有一个视频,我想从服务器下载,并流它在一个固定的视图。我在故事板中设置了一个带有固定约束的UIView,下面是我在代码中所做的:

@IBOutlet weak var videoView: UIView!
var player: AVPlayer!
var avpController = AVPlayerViewController()

在我的viewDidLoad中,我这样做了:

let url = URL(string:myURL)
player = AVPlayer(url: url!)

avpController.player = player
avpController.videoGravity = AVLayerVideoGravity.resizeAspect.rawValue
self.addChildViewController(avpController)
avpController.view.frame = videoView.frame
self.containerView.addSubview(avpController.view)
videoView.layer.masksToBounds = true

我的问题是我的视频不是我设置为videoView的大小,在每个设备中我的视频都是不同的大小。在某些设备中,视频高度大于我设置的高度,它覆盖了我在videoView以下的项目。如何以正确的方式在视图中播放视频?

kqhtkvqz

kqhtkvqz1#

如果要使用AVPlayerViewController:

let videoURL = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
let player = AVPlayer(url: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
    playerViewController.player!.play()
}

对于AVPlayer:

let videoURL = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
let player = AVPlayer(url: videoURL!)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = self.view.bounds
self.view.layer.addSublayer(playerLayer)
player.play()
cfh9epnr

cfh9epnr2#

将视频添加到UIView中的固定约束条件的唯一方法是:

let url = URL(string:myURL)

player = AVPlayer(url: url!)

avpController.player = player

avpController.view.frame.size.height = videoView.frame.size.height

avpController.view.frame.size.width = videoView.frame.size.width

self.videoView.addSubview(avpController.view)

我希望其他人可以使用这个!:)

nimxete2

nimxete23#

您可以尝试在AVPlayer中使用AVPlayerLayer

let player = AVPlayer(url: video) // your video url
      let playerLayer = AVPlayerLayer(player: player)
      playerLayer.frame = videoView.bounds
      videoView.layer.addSublayer(playerLayer)
      player.play()
kq4fsx7k

kq4fsx7k4#

这就是你如何在uiview中添加和删除播放器。

var player: AVPlayer?
var playerController: AVPlayerViewController?
func removePlayer() {
    player?.pause()
    player = nil
    playerController?.player?.pause()
    playerController?.player = nil
    if let view = playerController?.view {
        videoView.willRemoveSubview(view)
    }
    playerController?.view.removeFromSuperview()
    playerController = nil
}

func setVideo(url: URL) {
    removePlayer()
    player = AVPlayer(url: url)
    playerController = AVPlayerViewController()
    playerController?.player = player
    self.videoView.addSubview((playerController?.view)!)
    playerController?.view.frame = CGRect(x: 0, y: 0, width: self.videoView.bounds.width, height: self.videoView.bounds.height)
   // player.play()
}
fivyi3re

fivyi3re5#

import AVKit import AVFoundation import UIKit //类ViewController:UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
    let player = AVPlayer(url: URL(fileURLWithPath: Bundle.main.path(forResource: "shiv", ofType: "mp4")!))

    let vc = AVPlayerViewController()
    vc.player = player
    present(vc, animated: true)

相关问题