我的应用程序必须记录并识别用户的语音,并提供一些文本转语音功能。有点适合第一次录制会话,但显然在那之后就坏了(播放正常)。
我正在使用startRecording()
进行录制,而stopRecording()
一种。停止录制并
b。设置AVAudioSession的播放类别(因此,用户将可以使用其airpods收听)。
我得到的是什么
AVAudioSessionPortImpl.mm:56:ValidateRequiredFields: Unknown selected data source for Port Speaker (type: Speaker)
或AVAudioSessionPortImpl.mm:56:ValidateRequiredFields: Unknown selected data source for Port zzx’s AirPods (type: BluetoothA2DPOutput)
好的,代码。
func startRecording() throws {
guard speechRecogniser.isAvailable else {
return
}
if let recognitionTask = recognitionTask {
recognitionTask.cancel()
self.recognitionTask = nil
}
guard SFSpeechRecognizer.authorizationStatus() == .authorized else {
SFSpeechRecognizer.requestAuthorization({ _ in })
return
}
let audioSession = AVAudioSession.sharedInstance()
try audioSession.setCategory(AVAudioSession.Category.playAndRecord, mode: .spokenAudio, options: [.defaultToSpeaker])
//I've tried to add bluetooth options here, but to no avail
try audioSession.setActive(true, options: [.notifyOthersOnDeactivation])
recognitionRequest = SFSpeechAudioBufferRecognitionRequest()
let inputNode = audioEngine.inputNode
guard let recognitionRequest = recognitionRequest else {
throw SpeechControllerError.noAudioInput
}
recognitionTask = speechRecogniser.recognitionTask(with: recognitionRequest) { [unowned self] result, error in
if let result = result {
self.delegateSpeech?.speechController(self, didRecogniseText: result.bestTranscription.formattedString)
self.nameLabel.text = result.bestTranscription.formattedString
print(result.bestTranscription.formattedString)
}
if result?.isFinal ?? (error != nil) {
inputNode.removeTap(onBus: self.inputNodeBus)
}
}
let recordingFormat = inputNode.outputFormat(forBus: self.inputNodeBus)
inputNode.installTap(onBus: self.inputNodeBus, bufferSize: 1024, format: recordingFormat) { (buffer: AVAudioPCMBuffer, when: AVAudioTime) in
self.recognitionRequest?.append(buffer)
}
audioEngine.prepare()
try audioEngine.start()
}
和stopRecording()
func stopRecording() {
audioEngine.stop()
recognitionRequest?.endAudio()
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSession.Category.playback, mode: .default, options: [.allowBluetooth, .allowBluetoothA2DP, .defaultToSpeaker])
//if I don't do this, my app will just _lose_ airpods and will use speaker for playback after the first recording session
try audioSession.setActive(false, options: [.notifyOthersOnDeactivation])
//Set to false because I don't need this anymore
} catch {
}
}
很显然,我在这里做错了什么,但是怎么办?