【问题标题】:how to increase volume in AVSpeechUtterance in swift如何在 swift 中增加 AVSpeechUtterance 的音量
【发布时间】:2021-10-08 13:34:45
【问题描述】:

全部 当我在模拟器中运行时,我使用我的项目 AVSpeechUtterance 进行文本到语音的转换工作正常,但是当我在设备中运行时,语音音量在这里我的代码很慢......

        let utterance = AVSpeechUtterance(string: "Good night all")

    if UserDefaults.standard.string(forKey: "LNG") == "Eng"
    {
        utterance.voice = AVSpeechSynthesisVoice(language: "en-US")
    }
    else
    {
        utterance.voice = AVSpeechSynthesisVoice(language: "de-DE")
    }

    utterance.rate = AVSpeechUtteranceDefaultSpeechRate
    utterance.volume = 1
    let synth = AVSpeechSynthesizer()
    synth.speak(utterance)

【问题讨论】:

  • 我认为你不需要这两行代码:utterance.rate = AVSpeechUtteranceDefaultSpeechRateutterance.volume = 1
  • 我也删除了,但还是不行
  • 而且我使用耳机它工作正常,我也尝试其他设备问题是相同的声音非常慢

标签: ios swift avspeechutterance


【解决方案1】:

设备的默认值可能不同。你检查了吗?

您可以通过转到设置>辅助功能>语音内容来查看设备设置的内容

让用户有机会调整此费率,因为许多人的理想费率可能不同

import SwiftUI
import Speech
class SpeechManager:ObservableObject{
    //The default value is likely different for the devices
    //You can also look up what the device has set by going to Settings>Accessibility>Spoken Content
    var defaultRate = AVSpeechUtteranceDefaultSpeechRate
    //Stuff like this is the intended use of UserDefaults
    var userSpeechRate: Float{
        get{
            UserDefaults.standard.value(forKey: "userSpeechRate") as? Float ?? AVSpeechUtteranceDefaultSpeechRate
        }
        set{
            UserDefaults.standard.set(newValue, forKey: "userSpeechRate")
            objectWillChange.send()
        }
    }
    //You can save the volume too.
    //If your device doesn't respond to this change it is likely a hardware issue or a specific setting on the device.
    //Something like the device volume and Silent mode button being off
    var userSpeechVolume: Float{
        get{
            UserDefaults.standard.value(forKey: "userSpeechVolume") as? Float ?? 1
        }
        set{
            UserDefaults.standard.set(newValue, forKey: "userSpeechVolume")
            objectWillChange.send()
        }
    }
    //This is mostly your code with the exception of setting a custom rate
    func sayGoodNight(){
        let utterance = AVSpeechUtterance(string: "Good night all")
        
        if UserDefaults.standard.string(forKey: "LNG") == "Eng"
        {
            utterance.voice = AVSpeechSynthesisVoice(language: "en-US")
        }
        else
        {
            utterance.voice = AVSpeechSynthesisVoice(language: "de-DE")
        }
        //Change this to use the
        utterance.rate = userSpeechRate
        utterance.volume = userSpeechVolume
        let synth = AVSpeechSynthesizer()
        synth.speak(utterance)
    }
}
struct SpeechView: View {
    @StateObject var vm: SpeechManager = SpeechManager()
    var body: some View {
        VStack{
            Text("default rate \(vm.defaultRate)")
            Text("user rate \(vm.userSpeechRate)")
            Slider(value: $vm.userSpeechRate, in: 0...1, label: {Text("speech rate")})
            Slider(value: $vm.userSpeechVolume, in: 0...1, label: {Text("speech volume")})
            Button("say goodnight", action: {
                vm.sayGoodNight()
            })
        }
    }
}

struct SpeechView_Previews: PreviewProvider {
    static var previews: some View {
        SpeechView()
    }
}

【讨论】:

  • @marcgg 你试过代码了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 2017-01-18
  • 1970-01-01
  • 1970-01-01
  • 2014-09-26
  • 1970-01-01
相关资源
最近更新 更多