iOS:使用 SFSpeechRecognizer 录制后,AVSpeechSynthesizer 不起作用
<p><p>我正在制作一个执行文本到语音和语音到文本的应用程序。 </p>
<p>我现在遇到的问题是文本转语音使用 AVSpeechSynthesizer 可以正常工作。但是在我使用 SFSpeechRecognizer 录制并进行语音到文本之后,文本到语音停止工作(即不回话)。 </p>
<p>我也是 swift 新手。但是我从几个不同的教程中得到了这段代码,并试图将它们合并在一起。 </p>
<p>这是我的代码:</p>
<pre><code> private var speechRecognizer = SFSpeechRecognizer(locale: Locale.init(identifier: "en-US"))!
private var recognitionRequest: SFSpeechAudioBufferRecognitionRequest?
private var recognitionTask: SFSpeechRecognitionTask?
private var audioEngine = AVAudioEngine()
@objc(speak:location:date:callback:)
func speak(name: String, location: String, date: NSNumber,_ callback: @escaping (NSObject) -> ()) -> Void {
let utterance = AVSpeechUtterance(string: name)
let synthesizer = AVSpeechSynthesizer()
synthesizer.speak(utterance)
}
@available(iOS 10.0, *)
@objc(startListening:location:date:callback:)
func startListening(name: String, location: String, date: NSNumber,_ callback: @escaping (NSObject) -> ()) -> Void {
if audioEngine.isRunning {
audioEngine.stop()
recognitionRequest?.endAudio()
} else {
if recognitionTask != nil {//1
recognitionTask?.cancel()
recognitionTask = nil
}
let audioSession = AVAudioSession.sharedInstance()//2
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
try audioSession.setMode(AVAudioSessionModeMeasurement)
try audioSession.setActive(true, with: .notifyOthersOnDeactivation)
} catch {
print("audioSession properties weren't set because of an error.")
}
recognitionRequest = SFSpeechAudioBufferRecognitionRequest()//3
guard let inputNode = audioEngine.inputNode else {
fatalError("Audio engine has no input node")
}//4
guard let recognitionRequest = recognitionRequest else {
fatalError("Unable to create an SFSpeechAudioBufferRecognitionRequest object")
} //5
recognitionRequest.shouldReportPartialResults = true//6
recognitionTask = speechRecognizer.recognitionTask(with: recognitionRequest, resultHandler: { (result, error) in//7
var isFinal = false//8
if result != nil {
print(result?.bestTranscription.formattedString)//9
isFinal = (result?.isFinal)!
}
if error != nil || isFinal {//10
self.audioEngine.stop()
inputNode.removeTap(onBus: 0)
self.recognitionRequest = nil
self.recognitionTask = nil
}
})
let recordingFormat = inputNode.outputFormat(forBus: 0)//11
inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer, when) in
self.recognitionRequest?.append(buffer)
}
audioEngine.prepare()//12
do {
try audioEngine.start()
} catch {
print("audioEngine couldn't start because of an error.")
}
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>他们都有一个 AVAudioSession。 </p>
<p>对于 AVSpeechSynthesizer,我想它必须设置为:</p>
<pre><code>_audioSession.SetCategory(AVAudioSessionCategory.Playback,
AVAudioSessionCategoryOptions.MixWithOthers);
</code></pre>
<p>对于 SFSpeechRecognizer:</p>
<pre><code>_audioSession.SetCategory(AVAudioSessionCategory.PlayAndRecord,
AVAudioSessionCategoryOptions.MixWithOthers);
</code></pre>
<p>希望对你有帮助。</p></p>
<p style="font-size: 20px;">关于iOS:使用 SFSpeechRecognizer 录制后,AVSpeechSynthesizer 不起作用,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/43637714/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/43637714/
</a>
</p>
页:
[1]