问题
I am trying Speech recognition sample. If I started to recognise my speech via microphone, then I tried to get iPhone's voice of that recognised text. It is working. But, voice is too low. Can u guide me on this?
Rather than, if I am trying in simple button action, with AVSpeechUtterance
code, volume is normal.
After that, If I go for startRecognise()
method, volume is too low.
My Code
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.")
}
}
My Button Action
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.
}
回答1:
Finally, I got Solution.
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.")
}
...
}
Once I comment this line, try audioSession.setMode(AVAudioSessionModeMeasurement)
, volume is working normal.
来源:https://stackoverflow.com/questions/45371931/swift-iphones-volume-is-low-when-trying-to-change-speech-to-iphones-voice-in