ios - Swift:尝试快速将语音更改为 iPhone 的声音时,iPhone 的音量很低

标签 ios swift avaudioplayer avspeechsynthesizer sfspeechrecognizer

我正在尝试语音识别样本。如果我开始通过麦克风识别我的语音,那么我会尝试获取该识别文本的 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.
}

最佳答案

最后,我得到了解决方案。

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.")
    }

    ... 
}

一旦我评论了这一行,尝试 audioSession.setMode(AVAudioSessionModeMeasurement),音量正常。

关于ios - Swift:尝试快速将语音更改为 iPhone 的声音时,iPhone 的音量很低,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45371931/

相关文章:

ios - 在后台线程上创建 UIViewController 可以吗?

swift - 录制和播放声音在模拟器中效果很好,但在我的 iPhone 中则不然

ios - 在 swift 中,有什么方法可以调用 super.methodname 吗?

ios - 如何使用 Alamofire 为 http 请求编写单元测试?

ios - 在 GameViewController 中连接一个 UIButton 以在 SKScene (Swift) 中工作

ios - AVAudioSession 未检测到已连接的蓝牙设备

android - 如何在Android中播放Base64编码的音频?

ios - 在我的 iPhone 应用程序中将字符串转换为日期

objective-c - UIModalTransitionStyleFlipHorizo​​ntal 期间的状态栏背景

ios - 如何使用 NSNumbers 在 switch 语句中设置 NSDate