视频播放器:播放,暂停,前进和后退方法IOS Swift 3

时间:2018-07-04 07:29:57

标签: ios swift3 avplayer avplayerviewcontroller video-player

enter image description here

如何获取具有时间差的事件/委托(播放,暂停,前进和后退)按钮。

在这里,我将代码放在下面。请建议我们可以使用其他播放器进行此活动

player = AVPlayer(url:URL(fileURLWithPath: objpdfURL));
NotificationCenter.default.addObserver(self, selector: #selector(VedioPlayVC.didfinishplaying(note:)),name:NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem)

NotificationCenter.default.addObserver(self, selector:#selector(VedioPlayVC.Stop(note:)), name: .kAVPlayerViewControllerDismissingNotification, object: nil);

NotificationCenter.default.addObserver(self, selector: #selector(VedioPlayVC.Jump(note:)),name:NSNotification.Name.AVPlayerItemTimeJumped, object: nil)

playerController.player = player
playerController.allowsPictureInPicturePlayback = true
playerController.delegate = self
playerController.player?.play()
self.present(playerController,animated:true,completion:nil)

1 个答案:

答案 0 :(得分:0)

希望这会有所帮助

@IBAction func btnPlayAction(_ sender: UIButton) {
    if isVideoPlaying{
        player.pause()
        sender.setTitle("Play", for: .normal)
    }else{
        player.play()
        sender.setTitle("Pause", for: .normal)
    }
    isVideoPlaying = !isVideoPlaying
}

@IBAction func btnForwardAction(_ sender: UIButton) {
    guard let duration = player.currentItem?.duration else { return }
    let currentTime = CMTimeGetSeconds(player.currentTime())
    let newTime = currentTime + 5.0
    if newTime < (CMTimeGetSeconds(duration) - 5.0){
        let time: CMTime = CMTimeMake(value: Int64(newTime*1000), timescale: 1000)
        player.seek(to: time)
    }
}
@IBAction func btnBackwardAction(_ sender: UIButton) {

    let currentTime = CMTimeGetSeconds(player.currentTime())
    var newTime = currentTime - 5.0

    if newTime < 0{
        newTime = 0
    }
    let time: CMTime = CMTimeMake(value: Int64(newTime*1000), timescale: 1000)
    player.seek(to: time)

}