BDGapps
BDGapps

Reputation: 3356

AVAudioSession Swift

I am trying to write a swift iOS app that will record the users voice. I have wrote the following code in swift however it fails to request mic permissions from the user. It prints granted yet it never records audio and in the settings pane under privacy it does not list the app. How do I request recording permissions in swift?

var session: AVAudioSession = AVAudioSession.sharedInstance()
session.requestRecordPermission({(granted: Bool)-> Void in
     if granted {
          println(" granted")
          session.setCategory(AVAudioSessionCategoryPlayAndRecord, error: nil)
          session.setActive(true, error: nil)
          self.recorder.record()
     }else{
          println("not granted")
     }
})

Upvotes: 11

Views: 18278

Answers (3)

Shakeel Ahmed
Shakeel Ahmed

Reputation: 6013

Swift 5

var session: AVAudioSession = AVAudioSession.sharedInstance()
@IBAction func btnmike(_ sender: Any) {
   // let session = AVAudioSession.sharedInstance()
    if (session.responds(to: #selector(AVAudioSession.requestRecordPermission(_:)))) {
        AVAudioSession.sharedInstance().requestRecordPermission({(granted: Bool)-> Void in
            if granted {
                print("granted")

                do {
                    try self.session.setCategory(AVAudioSessionCategoryPlayAndRecord)
                    try self.session.setActive(true)
                }
                catch {

                    print("Couldn't set Audio session category")
                }
            } else{
                print("not granted")
            }
        })
    }
}

Upvotes: 4

neowinston
neowinston

Reputation: 7764

Since iOS 7 you need check if it responds to selector requestRecordPermission:

I've tested this code using an iPhone 5S with iOS 8 Beta and it works perfectly. Once you grant permission, the system won't ask for it again.

It's worth saying that it didn't ask for permission when using the Simulator.

This is the code I've tried and is working:

if (session.respondsToSelector("requestRecordPermission:")) {
    AVAudioSession.sharedInstance().requestRecordPermission({(granted: Bool)-> Void in
        if granted {
            println("granted")
            session.setCategory(AVAudioSessionCategoryPlayAndRecord, error: nil)
            session.setActive(true, error: nil)
            self.recorder ()
        } else{
            println("not granted")
        }
     })

}

Upvotes: 14

gabriel_vincent
gabriel_vincent

Reputation: 1250

For Swift 3:

let session = AVAudioSession.sharedInstance()
    if (session.responds(to: #selector(AVAudioSession.requestRecordPermission(_:)))) {
        AVAudioSession.sharedInstance().requestRecordPermission({(granted: Bool)-> Void in
            if granted {
                Linphone.manager.callUser(username: username)

                print("granted")

                do {
                    try session.setCategory(AVAudioSessionCategoryPlayAndRecord)
                    try session.setActive(true)
                }
                catch {

                    print("Couldn't set Audio session category")
                }
            } else{
                print("not granted")
            }
        })
    }

Upvotes: 5

Related Questions