Display timer and progress bar with AVAudioRecorder Swift

  • Drag and drop “Progress View” from the object Library into your view controller.
  • Link progress view with your view controller class.

    @IBOutlet weak var progressView: UIProgressView!


  • Add a label to your view controller and link it.

    @IBOutlet weak var recorderTimer: UILabel!

  • Declare a variable of type NSTimer in your view controller class.

var timer:NSTimer?

  • In viewDidLoad() function of your view controller class copy paste following code

        timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector:Selector("setProgress"), userInfo: nil, repeats: true)


  • Make a function setProgress()

  func setProgress() {

var seconds = (audioRecorder?.currentTime)

var time=String(format: "%0.0f sec",seconds!)

recorderTimer.text = time

var progresscounter:Double=(audioRecorder?.currentTime)!

progressView.progress=Float(progresscounter/10)

}

Leave a Reply

Your email address will not be published. Required fields are marked *