Supratik Majumdar
Supratik Majumdar

Reputation: 2466

Why to I need to declare an AVAudioSession instance?

@IBAction func recordAudio(sender: UIButton) {
recodingLabel.hidden = false
stopButton.hidden = false


let dirPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String

    let currentDateTime = NSDate()
    let formatter = NSDateFormatter()
    formatter.dateFormat = "ddMMyyyy-HHmmss"

    let recordingName = formatter.stringFromDate(currentDateTime)+".wav"

    let pathArray = [dirPath,recordingName]

    let filePath = NSURL.fileURLWithPathComponents(pathArray)
    //println(filePath)

  var recordSession = AVAudioSession.sharedInstance()
  recordSession.setCategory(AVAudioSessionCategoryPlayAndRecord, error: nil)

  audioRecorder = AVAudioRecorder(URL: filePath, settings: nil, error: nil)
  audioRecorder.delegate = self
  audioRecorder.meteringEnabled = true
  audioRecorder.record()

}

As from the above code is is visible that I am trying to record an audio from the user on the press of a button and on the next view I wish to play the same back.

The issue I have is with the following block of code:

var recordSession = AVAudioSession.sharedInstance()
recordSession.setCategory(AVAudioSessionCategoryPlayAndRecord, error: nil)

Why do I need to declare the above block of code? When I did comment out and run the app, there was no difference.

The above code is from an Udacity Course

Upvotes: 0

Views: 416

Answers (1)

user3821934
user3821934

Reputation:

Audio is a shared resource on a device. The reason for letting the system know what you are planning to do with Audio is to help it coordinate your usage with other things that are happening. For example, what happens to your App if the phone rings during a recording, or if the user selects a different microphone? These and other interactions are handled by the global AVAudioSession object.

Upvotes: 2

Related Questions