Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //this routine returns the url from control to the VC (delegate)
- func selectedAudio(_ audioUrl:String?) {
- if let urlString = audioUrl {
- setupPlayer(urlString)
- }
- }
- //single button which toggles between play/pause
- func playOrPauseTouched() {
- if player?.rate == 0 { //play, since player is stopped
- player?.play()
- isPlaying = true
- playPauseButton.setImage(UIImage(named: "pause"), for: .normal)
- activityIndicatorView.startAnimating()
- } else { // pause
- player?.pause()
- isPlaying = false
- playPauseButton.setImage(UIImage(named: "play"), for: .normal)
- }
- }
- func setupPlayer(_ urlString:String) {
- if let url = URL(string:urlString) {
- player = AVPlayer(url: url)
- let playerLayer = AVPlayerLayer(player: player)
- player?.addObserver(self, forKeyPath: "currentItem.loadedTimeRanges", options: .new, context: nil)
- let interval = CMTime(value: 1, timescale: 5) //gives smooth thumb movement w/short clips
- player?.addPeriodicTimeObserver(forInterval: interval, queue: DispatchQueue.main, using: { (progressTime) in
- let seconds = CMTimeGetSeconds(progressTime)
- let secondsString = String(format: "%02d", Int(seconds.truncatingRemainder(dividingBy: 60)))
- let minutesString = String(format: "%02d", Int(seconds / 60))
- self.currentTimeLabel.text = "(minutesString):(secondsString)"
- if let duration = self.player?.currentItem?.duration {
- let durationSeconds = CMTimeGetSeconds(duration)
- self.audioSlider.value = Float(seconds / durationSeconds)
- }
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement