我有一个问题,关于从AVPlayer暂停视频时如何在表视图中生成时间戳记

时间:2019-06-27 12:00:19

标签: ios iphone swift4 avplayer

我无法为视频暂停产生时间戳。我想有时间在我的笔记弹出窗口中暂停视频,以便用户知道他们何时记笔记。我有通过.addPeriodicTimeObserver在控制台中打印视频的时间。

我已经使用player.addPeriodicTimeObserver来打印视频在控制台中的时间,并尝试了多种方法将其放入cell.detailTextLabel.text中。任何帮助将不胜感激!

func playVideo(_ sender: Any) {


    guard let path = Bundle.main.path(forResource: "Hot Work Scene 1 Graphics Test 2 JR", ofType:"mp4") else {
        debugPrint("Video.mp4 not found")
        return
    }

    let player = AVPlayer(url: URL(fileURLWithPath: path))
    playerController.player = player

    self.addChild(playerController)
    self.view.addSubview(playerController.view)

    player.play()
    player.pause()
    let time_interval = CMTime(value: 1, timescale: 2)

    player.addPeriodicTimeObserver(forInterval: time_interval, queue: DispatchQueue.main, using: { (progressTime) in
        let current_time = String.duration(from: progressTime)
        print(current_time)

    })
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
    if cell == nil {
        cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")
    }
        cell?.textLabel?.text = noteData[indexPath.row]

        cell?.detailTextLabel?.text = String.duration(from: CMTime(value: 1, timescale: 2))


    return cell!
}

extension String {

    static func duration(from time: CMTime) -> String {
        let totalSeconds = CMTimeGetSeconds(time)
        let secondsText = String(format: "%02d", Int(totalSeconds) % 60)
        let minutesText = String(format: "%02d", Int(totalSeconds) / 60)
        return  "\(minutesText):\(secondsText)"


    }

}

该单元格的详细信息部分仅会在扩展程序提供时产生00:00,但我想将视频暂停到该位置的实际时间

0 个答案:

没有答案