AVAudioSession Swift

前端 未结 3 965
野趣味
野趣味 2020-12-19 02:27

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

3条回答
  •  天涯浪人
    2020-12-19 03:02

    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")
            }
         })
    
    }
    

提交回复
热议问题