【发布时间】:2016-07-15 09:12:17
【问题描述】:
这似乎是一个非常基本的问题,但我正在努力寻找答案。据我所知,AVAudioInputNode 自 iOS 8 以来就已经可用,例如,它可用于从 iPhone 上的麦克风进行录音。
我以前知道我会使用 AVAudioRecorderSession 来请求录制权限并检查我是否有权限等。我正在努力的是查看使用 AVAudioEngine 时如何请求和检查权限。
那么在下面这样的情况下,我该怎么做呢?
import AVFoundation
class ViewController: UIViewController {
let audioEngine = AVAudioEngine()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func recordPressed(sender:AnyObject) {
try! startRecording()
}
func startRecording() throws{
let audioSession = AVAudioSession.sharedInstance()
try audioSession.setCategory(AVAudioSessionCategoryRecord)
try audioSession.setMode(AVAudioSessionModeMeasurement)
try audioSession.setActive(true, withOptions: .NotifyOthersOnDeactivation)
guard let inputNode = audioEngine.inputNode else { fatalError("Audio engine has no input node") }
let recordingFormat = inputNode.outputFormatForBus(0)
inputNode.installTapOnBus(0, bufferSize: 1024, format: recordingFormat) { (buffer: AVAudioPCMBuffer, when: AVAudioTime) in
}
audioEngine.prepare()
try audioEngine.start()
}
}
我注意到可以在 plist 中设置麦克风隐私消息,并且调用 audioEngine.inputNode 似乎会显示此消息。但是我仍然看不到在哪里检查是否已授予该权限。如果我添加
if (audioSession.recordPermission() == AVAudioSessionRecordPermission.granted){
}else{
print("No permission")
}
在guard let inputNode 行之后直接显示消息,但总是告诉我在我响应警报之前未授予权限。
解决此问题的最佳方法是什么?我应该先回退到使用 audioSession 方法吗?值得一提的是,这是从 SpeechRecognizer 周围的 WWDC 代码中提取的。我只是看不到他们在哪里处理麦克风权限
事实上,它似乎并不总是显示请求权限的警报。我在这里缺少什么,因为我发现的示例似乎跳过了这个权限步骤
【问题讨论】:
标签: ios avaudiosession avaudioengine