AVAudioSession Swift [英] AVAudioSession Swift

查看:800
本文介绍了AVAudioSession Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个快速的iOS应用程序来记录用户的声音。我在swift中编写了以下代码,但它无法向用户请求mic权限。它打印已授予但它从不记录音频,并且在隐私下的设置窗格中,它不会列出应用程序。如何在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")
     }
})


推荐答案

从iOS 7开始,您需要检查它是否响应选择器 requestRecordPermission:

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

我使用 iPhone 5S iOS 8测试了此代码Beta ,效果很好。一旦您授予权限,系统将不会再次请求它。

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

}

这篇关于AVAudioSession Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆