Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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")
     }
})
like image 432
BDGapps Avatar asked Jun 20 '14 01:06

BDGapps


People also ask

What is Avkit in Swift?

A view controller that displays content from a player and presents a native user interface to control playback.

What is Avaudioengine?

An object that manages a graph of audio nodes, controls playback, and configures real-time rendering constraints.


1 Answers

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")
            }
        })
    }
like image 66
gabriel_vincent Avatar answered Oct 07 '22 00:10

gabriel_vincent