gpt4 book ai didi

ios - UIView 中的 AVPlayer

转载 作者:行者123 更新时间:2023-11-29 00:08:53 25 4
gpt4 key购买 nike

所以我在 HomeViewController 上有一个 View ,我想在其中播放视频。在 viewDidLoad() 上,我正在调用 playVideo() 这是我的代码:

func playVideo() {
guard let filePath = Bundle.main.path(forResource: "video", ofType: "mov") else {
debugPrint("video.mov not found.")
return
}

let avPlayer = AVPlayer(url: URL(fileURLWithPath: filePath))
let avPlayerController = AVPlayerViewController()
avPlayerController.player = avPlayer
avPlayerController.view.frame = playerView.bounds
avPlayerController.showsPlaybackControls = false
avPlayerController.player?.play()
playerView.addSubview(avPlayerController.view)
playerView.bringSubview(toFront: QRButton)
}

我得到的只是黑色背景,没有视频。我究竟做错了什么?我已经尝试解决这个问题一段时间了。

iOS:11.2 测试版
Xcode:9.2 测试版

最佳答案

尝试使用avPlayer作为类变量,可能还没开始播放就已经释放了。

class ViewController: UIViewController {
var avPlayer:AVPlayer?

func playVideo() {
guard let filePath = Bundle.main.path(forResource: "video", ofType: "mov") else {
debugPrint("video.mov not found.")
return
}

avPlayer? = AVPlayer(url: URL(fileURLWithPath: filePath))
let avPlayerController = AVPlayerViewController()
avPlayerController.player = avPlayer
avPlayerController.view.frame = playerView.bounds
avPlayerController.showsPlaybackControls = false
avPlayerController.player?.play()
playerView.addSubview(avPlayerController.view)
playerView.bringSubview(toFront: QRButton)
}
}

关于ios - UIView 中的 AVPlayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47188180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com