How To Match UISlider to Audio Progress

I am creating a simple music app, and I was wondering how I can make a UiSlider to follow the progress of a audio file like the Apple Music app. Here's my project so far:


import UIKit
import AVFoundation

class MusicDetailViewController: UITableViewController {

    var audioPlayer = AVAudioPlayer()

    override func viewDidLoad() {
        super.viewDidLoad()
    
        do {
        
            audioPlayer = try AVAudioPlayer(contentsOf: URL.init(fileURLWithPath: Bundle.main.path(forResource: "Song Name", ofType: "mp3")!))
        
            audioPlayer.prepareToPlay()
        
            var audioSession = AVAudioSession.sharedInstance()
        
            do {
            
                try audioSession.setCategory(AVAudioSessionCategoryPlayback)
            
            }
        
        }
        
        catch {
        
            print(error)
        
        }
    }

    @IBAction func dismiss(_ sender: Any) {
        dismiss(animated: true, completion: nil)
    }

    @IBAction func play(_ sender: Any) {
        audioPlayer.stop()
        audioPlayer.play()
    }

    @IBAction func pause(_ sender: Any) {
        audioPlayer.pause()
    }

    @IBAction func restart(_ sender: Any) {
        audioPlayer.currentTime = 0
    }

}



My storyboard looks like this: https://i.stack.imgur.com/eQj00.png


I'm wanting to create the uislider similar to the Apple Music app where it follows the audio file's progress and whenever the user slides the ball thing (lol) it goes to that time of the song. If you could give me some code to complete this, that would be amazing!

Please keep in mind that I am fairly new to coding and am still learning swift, so keep it simple :-) Thanks again!

Replies

I think you should use UIProgressView.


have a look at this example:

http : / / rshankar.com/swift-demo-add-progress-bar/