gpt4 book ai didi

ios - 如何从自定义 View 过渡到 AVPlayerViewController?

转载 作者:行者123 更新时间:2023-11-28 11:59:20 30 4
gpt4 key购买 nike

我有一个自定义 View :

class MediaPlayerView: UIView {

var mediaURL: URL? {
didSet {
determineMediaType()
}
}
let videoExtensions = ["mov"]

override init (frame : CGRect) {
super.init(frame : frame)
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

func determineMediaType() {
let url = self.mediaURL
let pathExtention = url?.pathExtension
if videoExtensions.contains(pathExtention!) {
print("Movie URL: \(String(describing: url))")
setupVideo(url: url!)
} else {
print("Image URL: \(String(describing: url))")
setupImage(url: url!)
}
}

func setupVideo(url: URL) {
let playButton = UIImage(named: "Play Triangle")
let playButtonView = UIImageView(image: playButton!)
let singleTap = UITapGestureRecognizer(target: self, action: #selector(tapDetected))
playButtonView.addGestureRecognizer(singleTap)
playButtonView.center = self.center
playButtonView.frame.size.width = self.frame.size.width/5
playButtonView.frame.size.height = self.frame.size.height/5
playButtonView.autoresizingMask = [.flexibleWidth,.flexibleHeight]
playButtonView.isUserInteractionEnabled = true
self.addSubview(playButtonView)
}

@objc func tapDetected() {
print("tap!")

let player = AVPlayer(url: self.mediaURL!)
let controller = AVPlayerViewController()
controller.player = player
self.window?.rootViewController?.present(controller, animated: true) {
player.play()
}
}

func setupImage(url: URL) {
let imageView = UIImageView()
imageView.frame = self.bounds
imageView.autoresizingMask = [.flexibleWidth,.flexibleHeight]
self.addSubview(imageView)
imageView.kf.setImage(with: url)
}

}

但是当我点击播放按钮时,出现以下错误:

Warning: Attempt to present <AVPlayerViewController: 0x7fe8b200b000> on <SweatNet.MainTabBarController: 0x7fe8b4816400> whose view is not in the window hierarchy!

它来自这一行:self.window?.rootViewController?.present。我认为我调用 rootViewController(似乎是 SweatNet.MainTabBarController)让我感到困惑。我想让它调用 SweatNet.TagViewController。这是包含自定义 MediaPlayerView 的单元格的那个,但我不明白如何获得对此的引用。

最佳答案

试试这个:

class func topViewController(controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let navigationController = controller as? UINavigationController {
return topViewController(controller: navigationController.visibleViewController)
}
if let tabController = controller as? UITabBarController {
if let selected = tabController.selectedViewController {
return topViewController(controller: selected)
}
}
if let presented = controller?.presentedViewController {
return topViewController(controller: presented)
}
return controller
}

将你的方法写成:

@objc func tapDetected() {
print("tap!")

let player = AVPlayer(url: self.mediaURL!)
let controller = AVPlayerViewController()
controller.player = player
topViewController().present(controller, animated: true) {
player.play()
}
}

这将返回层次结构中的顶级 vc。希望对您有所帮助!

关于ios - 如何从自定义 View 过渡到 AVPlayerViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50381534/

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