gpt4 book ai didi

ios - 语音识别和音频播放可以在模拟器上运行,但不能在 iPhone 上运行?

转载 作者:行者123 更新时间:2023-11-30 10:52:14 30 4
gpt4 key购买 nike

我正在尝试使用语音识别将其转换为文本并显示。语音识别和音频播放可以在模拟器上运行,但不能在 iPhone 上运行?为什么会这样 ?

尝试在 iPhone 上进行识别时,我在控制台中收到此错误:

Domain=kAFAssistantErrorDomain Code=203 "Corrupt" UserInfo={NSLocalizedDescription=Corrupt, NSUnderlyingError=0x28063f240 {Error Domain=SiriSpeechErrorDomain Code=102 "(null)"}}

这是我的代码:

func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
self.animateSpinner(forStatus: false)
do {
try audioPlayer = AVAudioPlayer(contentsOf: recorder.url)
audioPlayer.play()
} catch let error {
debugPrint(error)
}

SFSpeechRecognizer.requestAuthorization({ (authStatus) in
if authStatus == .authorized {
let recognizer = SFSpeechRecognizer(locale: Locale(identifier: self.language))
let request = SFSpeechURLRecognitionRequest(url: recorder.url)
recognizer?.recognitionTask(with: request, resultHandler: { (result, err) in
if let err = err {
debugPrint(err)
return
}
self.textView.text = result?.bestTranscription.formattedString
})
}
})
}

这是 viewDidLoad() 中记录器的设置

func setupRecorder() {
let dirPaths = FileManager.default.urls(for: .documentDirectory,
in: .userDomainMask)

let soundFileURL = dirPaths[0].appendingPathComponent("sound.caf")
do {
audioRecorder = try AVAudioRecorder(url: soundFileURL, settings: [
AVEncoderAudioQualityKey: AVAudioQuality.min.rawValue,
AVEncoderBitRateKey: 16,
AVNumberOfChannelsKey: 2,
AVSampleRateKey: 44100.0])
audioRecorder.delegate = self
audioRecorder.prepareToRecord()
}
catch let error {
debugPrint(error)
}
}

最佳答案

viewDidLoad 中调用此方法修复了问题:

var audioSession: AVAudioSession = AVAudioSession.sharedInstance()

do {
try audioSession.setCategory(AVAudioSession.Category.playAndRecord, mode: .spokenAudio, options: .defaultToSpeaker)
try audioSession.setActive(true, options: .notifyOthersOnDeactivation)
} catch let error{
print("audioSession properties weren't set because of an error: ", error)
}

关于ios - 语音识别和音频播放可以在模拟器上运行,但不能在 iPhone 上运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54338008/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com