从一个单元格(不同视图和一个avcontroller)隔离到不同的控制器

时间:2019-10-19 14:16:11

标签: ios swift avfoundation avkit

在单元格中有视频时,我只想显示视频(在avPlayerViewController中),如果没有视频,则它会执行到详细视图的搜索

我已经尝试过了:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {


    let post = postArray[indexPath.item]
    let controller = PostDetailViewController.fromStoryboard(post: post)
    self.navigationController?.pushViewController(controller, animated: true)


    guard let videoURL = URL(string: post.videoLink)  else {
        return
            }

    let avPlayer = AVPlayer(url: videoURL)
    let avController = AVPlayerViewController()
    avController.player = avPlayer

    present(avController, animated: true) {
        avPlayer.play()
    }

}

如果有一个视频(来自Firebase),它将播放视频,但也会打开详细视图。因此,我希望该单元仅打开视频或仅打开detailview

1 个答案:

答案 0 :(得分:1)

对其进行结构化,以便在没有post.videoLink的情况下按PostDetailViewController,否则它将播放视频:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    guard let videoURL = URL(string: post.videoLink) else {
        //If there is no URL, then this else condition will be called. Otherwise, the code below the guard statement will be called, and the video player will appear.
        let post = postArray[indexPath.item]
        let controller = PostDetailViewController.fromStoryboard(post: post)
        self.navigationController?.pushViewController(controller, animated: true)
        return
    }

    let avPlayer = AVPlayer(url: videoURL)
    let avController = AVPlayerViewController()
    avController.player = avPlayer

    present(avController, animated: true) {
        avPlayer.play()
    }

}