【问题标题】:AVSpeechSynthesizer plays sound with phone call speaker instead of bottom speakerAVSpeechSynthesizer 使用电话扬声器而不是底部扬声器播放声音
【发布时间】:2021-10-03 17:39:42
【问题描述】:

我创建了一个函数来读取文本并在 iPhone 上使用底部扬声器播放,因为底部音量高于电话扬声器,但它使用低音量的电话扬声器播放。

@discardableResult
    func speechSentence(_ text: String) -> Bool {
        var utterance: AVSpeechUtterance!
        let synthesizer = AVSpeechSynthesizer()
        utterance = AVSpeechUtterance(string: text)
        utterance.voice = AVSpeechSynthesisVoice(language: "en-US")
        utterance.rate = AVSpeechUtteranceDefaultSpeechRate
        synthesizer.speak(utterance)
        
        return true
    }

项目中没有任何其他代码或配置。

我该如何解决这个问题?

【问题讨论】:

  • 你是如何配置 AVAudioSession 的?
  • @matt。我没有使用AVAudioSession ,因为我只想说出一个文本,这里是我使用的所有与语音到文本相关的代码。
  • 声音就是声音。如果您想指定声音的来源,AVAudioSession 就是您的方式。

标签: ios swift avfoundation avspeechutterance


【解决方案1】:

我发现了问题。我也必须配置AVAudioSession。我只是在我的代码中添加以下行并且它起作用了。

try? AVAudioSession.sharedInstance().setCategory(.playback, mode: .voicePrompt, options: [])

最终代码为:

@discardableResult
    func speechSentence(_ text: String) -> Bool {
        try? AVAudioSession.sharedInstance().setCategory(.playback, mode: .voicePrompt, options: [])
        var utterance: AVSpeechUtterance!
        let synthesizer = AVSpeechSynthesizer()
        utterance = AVSpeechUtterance(string: text)
        utterance.voice = AVSpeechSynthesisVoice(language: "en-US")
        utterance.rate = AVSpeechUtteranceDefaultSpeechRate
        synthesizer.speak(utterance)
        
        return true
    }

【讨论】:

  • 发现了问题是不对的。我告诉了你出了什么问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
  • 2017-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-02
相关资源
最近更新 更多