如何从自定义视图转换到AVPlayerViewController?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (322)

我有一个自定义视图:

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

}

但是,当我单击Play按钮时,会得到以下错误:

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

它出现在这一行上:Sel.Window?.rootViewController?.Present。我想它被我调用rootViewController(它似乎是SweatNet.MainTabBarController)弄糊涂了。我想打电话给SweatNet.agViewController。这是一个包含自定义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系统的一个例子。我最终意识到,我试图将视图控制器代码放入自定义视图中。视图控制器代码是启动AVPlayerViewController的代码。相反,我应该将这个逻辑放在我的视图控制器中。

我所做的工作是在视图控制器的内部使用CollectionView委托方法didSelectItemAt indexPath:IndexPath,其中集合视图的视图控制器逻辑保持如下:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if collectionView == self.collectionView {
        let post = posts[indexPath.row]
        if post.isVideo == true {
            self.performSegue(withIdentifier: "playVideo", sender: nil)
        } else {
            print("is image. might go full screen one day here")
        }
}

这是可行的,我相信这比编写视图的自定义方面来启动AVPlayerViewController要好。但这两种方法也可能奏效-我真的不太了解iOS的开发情况。

所属标签

可能回答问题的人

  • 应用案例分享

    1 粉丝490 提问6 回答
  • o o

    4 粉丝495 提问5 回答
  • 找虫虫

    5 粉丝0 提问5 回答
  • uncle_light

    5 粉丝518 提问4 回答

扫码关注云+社区

领取腾讯云代金券