如何在uitableview单元格中使用AVPlayer播放暂停视频

时间:2017-11-15 15:51:54

标签: ios swift

我有一个tableview,当它的单元格被点击时,它会在它下面添加一个新单元并播放一个视频,然后它会在整个屏幕中添加一个空白UIView,但它最初是隐藏的。当点击该单元格时,UIView不会隐藏,并且视频成功播放,当点击UIView时,它将转到AVPlayer.Pause()并删除正在播放视频的单元格。单元格已删除,但音频仍在播放。我添加了一个断点,Pause()已经运行。但它根本不起作用。 这是代码:

class Detail_ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

    var myPlan: PlansInfo!
    var planItems: [[[String]]] = []
    var videoIndexPath: IndexPath? = nil
    var avPlayer: AVPlayer?

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var view_MengBan: UIView!
    @IBOutlet weak var label_PlanName: UILabel!
    @IBOutlet weak var label_PlanIntroduce: UILabel!
    @IBOutlet weak var imageView_Detail: UIImageView!
    @IBOutlet weak var label_PlanTime: UILabel!


    func PauseVideo(avPlayer: AVPlayer) {
        avPlayer.pause()
    }

    @IBAction func TapToDeleteVideoGesture(_ sender: UITapGestureRecognizer) {
        if videoIndexPath != nil {

            avPlayer?.pause()

            planItems[0][(videoIndexPath?.section)!].remove(at: (videoIndexPath?.row)!)
            planItems[1][(videoIndexPath?.section)!].remove(at: (videoIndexPath?.row)!)
            tableView.deleteRows(at: [videoIndexPath!], with: .automatic)

            videoIndexPath = nil //让视频选项为空,使得视频能再次被打开
            view_MengBan.isHidden = true //让蒙板层隐藏,使手势关闭
            print("Video Removed")

        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        view_MengBan.isHidden = true

        let amp = AutoMakePlan(key_Kind: myPlan.key_Kind, key_Level: myPlan.key_Level, key_Days: myPlan.key_Days, key_Time: myPlan.key_Time, key_CanJuggle: myPlan.key_CanJuggle)
        label_PlanName.text = myPlan.planName
        label_PlanTime.text = String(myPlan.actualPlanTime) + " min"
        label_PlanIntroduce.text = amp.TranslateKey(key: myPlan.planKey!)?.planIntroduce
        imageView_Detail.image = UIImage(named: "Cristiano Ronaldo")

        planItems = amp.GetPlanItems(key: myPlan.planKey!, planTime: myPlan.planTime)!

        if #available(iOS 11.0, *) {
            self.navigationItem.largeTitleDisplayMode = .never
        } else {
            // Fallback on earlier versions
        }

        self.title = "Overview"

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table View

    func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 3
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        switch section {
        case 0:
            return planItems[0][0].count
        case 1:
            return planItems[0][1].count
        case 2:
            return planItems[0][2].count
        default:
            return 0
        }
    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! Detail_TableViewCell
        if planItems[0][indexPath.section][indexPath.row].contains("video") {
            cell.view_Video.isHidden = false

            let pathFile = Bundle.main.path(forResource: "Video", ofType: "mp4")
            let videoUrl = URL(fileURLWithPath: pathFile!)
            avPlayer = AVPlayer(url: videoUrl)
            let playerLayer = AVPlayerLayer(player: avPlayer)
            playerLayer.frame = cell.view_Video.frame
            cell.view_Video.layer.addSublayer(playerLayer)
            avPlayer?.play()
        }
        else{
            avPlayer = nil
            cell.view_Video.isHidden = true//Hide Video View
            cell.label_ItemName.text = planItems[0][indexPath.section][indexPath.row]
            cell.label_ItemTime.text = "\(planItems[1][indexPath.section][indexPath.row]) s"
        }
        return cell
    }

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        switch section {
        case 0:
            return "Warm Up"
        case 1:
            return "Training"
        case 2:
            return "Relax"
        default:
            return "Unkonw"
        }
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if planItems[0][indexPath.section][indexPath.row].contains("video") {
            return view.frame.width * (9/16) //let w:h = 16:9
        }
        return 50
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)

        if videoIndexPath == nil {
            planItems[0][indexPath.section].insert("video", at: indexPath.row + 1)
            planItems[1][indexPath.section].insert("video", at: indexPath.row + 1)
            tableView.insertRows(at: [IndexPath(row: indexPath.row + 1, section: indexPath.section)], with: .automatic)
            videoIndexPath = IndexPath(row: indexPath.row + 1, section: indexPath.section)
            tableView.scrollToRow(at: videoIndexPath!, at: .middle, animated: true)//使视图居中
            //蒙板层出现,手势开启
            view_MengBan.isHidden = false
        }

    }

0 个答案:

没有答案