4
我正在嘗試語音識別示例。如果我開始通過麥克風識別我的講話,那麼我試圖讓iPhone聽到那個可識別文本的聲音。這是工作。但是,聲音太低。你能指導我嗎?斯威夫特:試圖迅速改變語音到iPhone的語音時,iPhone的音量很低
而不是,如果我試圖在簡單的按鈕操作,AVSpeechUtterance
代碼,音量是正常的。
之後,如果我採用startRecognise()
方法,音量太低。
我的代碼
func startRecognise()
{
let audioSession = AVAudioSession.sharedInstance() //2
do
{
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
try audioSession.setMode(AVAudioSessionModeDefault)
try audioSession.setMode(AVAudioSessionModeMeasurement)
try audioSession.setActive(true, with: .notifyOthersOnDeactivation)
try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.speaker)
}
catch
{
print("audioSession properties weren't set because of an error.")
}
recognitionRequest = SFSpeechAudioBufferRecognitionRequest()
guard let inputNode = audioEngine.inputNode else {
fatalError("Audio engine has no input node")
}
guard let recognitionRequest = recognitionRequest else {
fatalError("Unable to create an SFSpeechAudioBufferRecognitionRequest object")
}
recognitionRequest.shouldReportPartialResults = true
recognitionTask = speechRecognizer.recognitionTask(with: recognitionRequest, resultHandler: { (result, error) in
if result != nil
{
let lastword = result?.bestTranscription.formattedString.components(separatedBy: " ").last
if lastword == "repeat" || lastword == "Repeat"{
self.myUtterance2 = AVSpeechUtterance(string: "You have spoken repeat")
self.myUtterance2.rate = 0.4
self.myUtterance2.volume = 1.0
self.myUtterance2.pitchMultiplier = 1.0
self.synth1.speak(self.myUtterance2)
// HERE VOICE IS TOO LOW.
}
}
})
let recordingFormat = inputNode.outputFormat(forBus: 0) //11
inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer, when) in
self.recognitionRequest?.append(buffer)
}
audioEngine.prepare()
do
{
try audioEngine.start()
}
catch
{
print("audioEngine couldn't start because of an error.")
}
}
我的按鈕操作
func buttonAction()
{
self.myUtterance2 = AVSpeechUtterance(string: "You are in button action")
self.myUtterance2.rate = 0.4
self.myUtterance2.volume = 1.0
self.myUtterance2.pitchMultiplier = 1.0
self.synth1.speak(self.myUtterance2)
// Before going for startRecognise() method,
//I tried with buttonAction(),
//this time volume is normal.
//After startRecognise() method call, volume is too low in both methods.
}
AVAudioSessionCategoryOptionDefaultToSpeaker - 添加此項 – YaBoiSandeep