Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. //this routine returns the url from control to the VC (delegate)
  2. func selectedAudio(_ audioUrl:String?) {
  3. if let urlString = audioUrl {
  4. setupPlayer(urlString)
  5. }
  6. }
  7.  
  8. //single button which toggles between play/pause
  9. func playOrPauseTouched() {
  10. if player?.rate == 0 { //play, since player is stopped
  11. player?.play()
  12. isPlaying = true
  13. playPauseButton.setImage(UIImage(named: "pause"), for: .normal)
  14. activityIndicatorView.startAnimating()
  15. } else { // pause
  16. player?.pause()
  17. isPlaying = false
  18. playPauseButton.setImage(UIImage(named: "play"), for: .normal)
  19. }
  20. }
  21.  
  22. func setupPlayer(_ urlString:String) {
  23. if let url = URL(string:urlString) {
  24. player = AVPlayer(url: url)
  25. let playerLayer = AVPlayerLayer(player: player)
  26. player?.addObserver(self, forKeyPath: "currentItem.loadedTimeRanges", options: .new, context: nil)
  27. let interval = CMTime(value: 1, timescale: 5) //gives smooth thumb movement w/short clips
  28. player?.addPeriodicTimeObserver(forInterval: interval, queue: DispatchQueue.main, using: { (progressTime) in
  29. let seconds = CMTimeGetSeconds(progressTime)
  30. let secondsString = String(format: "%02d", Int(seconds.truncatingRemainder(dividingBy: 60)))
  31. let minutesString = String(format: "%02d", Int(seconds / 60))
  32. self.currentTimeLabel.text = "(minutesString):(secondsString)"
  33. if let duration = self.player?.currentItem?.duration {
  34. let durationSeconds = CMTimeGetSeconds(duration)
  35. self.audioSlider.value = Float(seconds / durationSeconds)
  36. }
  37. })
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement