Swift: iPhone's volume is low when trying to change speech to iPhone's voice in swift

后端 未结 2 987
梦谈多话
梦谈多话 2021-01-02 18:56

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, v

相关标签:
2条回答
  • 2021-01-02 19:12

    After digging into the technical details its observed that, overrideOutputAudioPort() temporarily changes the current audio route.

    func overrideOutputAudioPort(_ portOverride: AVAudioSession.PortOverride) throws
    

    If your app uses the playAndRecord category, calling this method with the AVAudioSession.PortOverride.speaker option causes audio to be routed to the built-in speaker and microphone regardless of other settings.

    This change remains in effect only until the current route changes or you call this method again with the AVAudioSession.PortOverride.none option.

    try audioSession.setMode(AVAudioSessionModeDefault)
    

    If you would prefer to permanently enable this behavior, you should instead set the category's defaultToSpeaker option. Setting this option will always route to the speaker rather than receiver if no other accessory such as headphones are in use.

    In Swift 5.x above code looks like -

    let audioSession = AVAudioSession.sharedInstance()
    do {
      try audioSession.setCategory(.playAndRecord)
      try audioSession.setMode(.default)
      try audioSession.setActive(true, options: .notifyOthersOnDeactivation)
      try audioSession.overrideOutputAudioPort(.speaker)
    } catch {
      debugPrint("Enable to start audio engine")
      return
    }
    

    By setting mode to the measurement, its responsible for minimize the amount of system-supplied signal processing to input and output signals.

    try audioSession.setMode(.measurement)
    

    By commenting this mode and using default mode responsible for permanently enabling the audio route to the built-in speaker and microphone.

    Thanks @McDonal_11 for you answer. Hope this will helps to understand the technical details.

    0 讨论(0)
  • 2021-01-02 19:23

    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.

    0 讨论(0)
提交回复
热议问题