gpt4 book ai didi

ios - 自从向文本添加语音(语音识别)以来,文本到语音的音量显着下降

转载 作者:行者123 更新时间:2023-11-28 07:26:20 28 4
gpt4 key购买 nike

即使只是将文本从 UITextView 传输到 UILabel 时,带有标签的 View Controller 中的音量也变得非常安静。

除了音量问题外,几乎所有事情都运行良好。

语音类文件:

import UIKit
import AVFoundation

class TextToSpeech {

private let synthesizer = AVSpeechSynthesizer()
var Rate: Float = AVSpeechUtteranceDefaultSpeechRate
var Voice = AVSpeechSynthesisVoice(language: "en-US")

func Say(_ phrase: String) {
let Utterance = AVSpeechUtterance(string: phrase)
Utterance.rate = Rate
Utterance.voice = Voice

synthesizer.speak(Utterance)
}
}

文本转语音 Controller :

import UIKit
import AVFoundation

class TextToSpeechTest: UIViewController {

let speak = TextToSpeech()
let label = UILabel()

override func viewDidLoad(){ super.viewDidLoad()

speak.Say(label.text!)
}
}

语音到文本 Controller :

import UIKit
import AVFoundation
import Speech

class SpeechToText: UIViewController {

let textView = UITextView()
let audioEngine = AVAudioEngine()
let speechRecognizer: SFSpeechRecognizer? = SFSpeechRecognizer()
let request = SFSpeechAudioBufferRecognitionRequest()
var recognitionTask: SFSpeechRecognitionTask?
let speechToTextButton = UIButton()
let textToSpeechButton = UIButton()

func recordAndConvertSpeech() {
let node = audioEngine.inputNode
let recordingFormat = node.outputFormat(forBus: 0)
node.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) {
buffer, _ in self.request.append(buffer)
}

audioEngine.prepare()
do {
try audioEngine.start()
} catch {
return print(error)
}

guard let myRecognizer = SFSpeechRecognizer()
else { return }

if !myRecognizer.isAvailable { return }

recognitionTask = speechRecognizer?.recognitionTask(with: request, resultHandler: { result, error in

if let result = result {
let bestString = result.bestTranscription.formattedString

self.textView.text = bestString
} else if let error = error {
print(error)
}
})
}

@objc func speechToTextButton() {
recordAndConvertSpeech()
}

@objc func textToSpeechButton() {
let textToSpeechTest = TextToSpeechTest()

self.navigationController?.pushViewController(textToSpeechTest, animated: true)
textToSpeechTest.label.text = textView.text
}
}

只希望音量正常。在我添加语音识别之前一切正常。

最佳答案

发生这种情况是因为您打开麦克风,当麦克风打开时,系统会降低扬声器以防止递归噪音。

麦克风开启时可用的一些选项:- 使用耳机扬声器- 使用免提

因为如果扬声器继续保持其原始响亮清晰的声音,它会不断通过麦克风听到自己的声音。

关于ios - 自从向文本添加语音(语音识别)以来,文本到语音的音量显着下降,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56514413/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com