Add custom controls to AVPlayer in swift

白昼怎懂夜的黑 提交于 2019-11-27 07:54:36

here I add the points , you need to customize based on your need.

Step-1

initially hide your AVPlayer controls,

 YourAVPlayerViewController.showsPlaybackControls = false

Step-2

create the structure like

one label for current Duration, One label for overall Duration, one UIbutton for pause and play your current player and one UISlider for seek The video.

step-3

initially close the simple steps.

first stop and play the player using button action , currentPlayer is your AVPlayer name.

@IBAction func handlePlayPauseButtonPressed(_ sender: UIButton) {
   //  sender.isSelected ?  currentPlayer.pause() :   currentPlayer.play()
    if sender.isSelected {
        currentPlayer.pause()
    }
    else {
        currentPlayer.play()
    }
}

second set the video duration, as like

    let duration : CMTime = currentPlayer.currentItem!.asset.duration
    let seconds : Float64 = CMTimeGetSeconds(duration)

    lblOverallDuration.text = self.stringFromTimeInterval(interval: seconds)

third set the player current time to current duration label

    let duration : CMTime = currentPlayer.currentTime()
    let seconds : Float64 = CMTimeGetSeconds(duration)

    lblcurrentText.text = self.stringFromTimeInterval(interval: seconds)

the following method is convert from NSTimeinterval to HH:MM:SS

func stringFromTimeInterval(interval: TimeInterval) -> String {

    let interval = Int(interval)
    let seconds = interval % 60
    let minutes = (interval / 60) % 60
    let hours = (interval / 3600)
    return String(format: "%02d:%02d:%02d", hours, minutes, seconds)
}

finally we go for slider control for calulate the seek time

_playheadSlider.addTarget(self, action: #selector(self.handlePlayheadSliderTouchBegin), for: .touchDown)
_playheadSlider.addTarget(self, action:    #selector(self.handlePlayheadSliderTouchEnd), for: .touchUpInside)
_playheadSlider.addTarget(self, action: #selector(self.handlePlayheadSliderTouchEnd), for: .touchUpOutside)
_playheadSlider.addTarget(self, action: #selector(self.handlePlayheadSliderValueChanged), for: .valueChanged)

lets we go for action, initially when touchbegin is start then stop the player

handlePlayheadSliderTouchBegin

@IBAction func handlePlayheadSliderTouchBegin(_ sender: UISlider) {
currentPlayer.pause()
}

set the current item label for calculate the sender.value * CMTimeGetSeconds(currentPlayer.currentItem.duration)

@IBAction func handlePlayheadSliderValueChanged(_ sender: UISlider) {

        let duration : CMTime = currentPlayer.currentItem!.asset.duration
     let seconds : Float64 = CMTimeGetSeconds(duration) * sender.value
 //   var newCurrentTime: TimeInterval = sender.value * CMTimeGetSeconds(currentPlayer.currentItem.duration)
lblcurrentText.text = self.stringFromTimeInterval(interval: seconds)
   }

finally move the player based on seek

 @IBAction func handlePlayheadSliderTouchEnd(_ sender: UISlider) {

  let duration : CMTime = currentPlayer.currentItem!.asset.duration
var newCurrentTime: TimeInterval = sender.value * CMTimeGetSeconds(duration)
var seekToTime: CMTime = CMTimeMakeWithSeconds(newCurrentTime, 600)
currentPlayer.seek(toTime: seekToTime)
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!