嗨,我遇到了 Azure TTS 的问题当我尝试使用 python 文本到语音快速入门
示例时,我收到此错误
...> python example.py
Enter some text that you want to speak >
test
Speech synthesis canceled: CancellationReason.Error
Error details: USP error: timeout waiting for the first audio chunk
Did you set the speech resource key and region values?
使用Python 3.11.4 **Azure 语音转文本工作正常 **
最佳答案
我遵循了给定的MS Doc我得到了带有输入文本的语音输出。
代码:
import os
import azure.cognitiveservices.speech as speechsdk
speech_config = speechsdk.SpeechConfig(subscription=os.environ.get('SPEECH_KEY'), region=os.environ.get('SPEECH_REGION'))
audio_config = speechsdk.audio.AudioOutputConfig(use_default_speaker=True)
speech_config.speech_synthesis_voice_name='en-US-JennyNeural'
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config)
print("Enter text that you want to speak >")
text = input()
speech_synthesis_result = speech_synthesizer.speak_text_async(text).get()
if speech_synthesis_result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
print("Speech synthesized for text [{}]".format(text))
elif speech_synthesis_result.reason == speechsdk.ResultReason.Canceled:
cancellation_details = speech_synthesis_result.cancellation_details
print("Speech synthesis canceled: {}".format(cancellation_details.reason))
if cancellation_details.reason == speechsdk.CancellationReason.Error:
if cancellation_details.error_details:
print("Error details: {}".format(cancellation_details.error_details))
print("Did you set speech resource key and region?")
<强>。环境:
SPEECH_KEY="<specch_key>"
SPEECH_REGION="<speech_region>"
在运行上述代码之前,您需要在系统环境变量中设置SPEECH_KEY和SPEECH_REGION,如下所示,
输出:
运行成功如下,
然后,我输入一行Hi kamali, How are you。 kasimbi 怎么样?如下所示,它提供语音输出。
作为替代方案,您还可以尝试下面的其他文本转语音代码,
代码:
import os
from azure.cognitiveservices.speech import AudioDataStream, ResultReason, SpeechConfig, SpeechSynthesizer, SpeechSynthesisOutputFormat
subscription_key = "<speech_key>"
region = "<speech_region>"
speech_config = SpeechConfig(subscription=subscription_key, region=region)
synthesizer = SpeechSynthesizer(speech_config=speech_config)
try:
text = input("Enter some text that you want to speak > ")
result = synthesizer.speak_text_async(text).get()
if result.reason == ResultReason.SynthesizingAudioCompleted:
print("Speech synthesis succeeded")
else:
print("Speech synthesis failed: {}".format(result.reason))
except Exception as e:
print("An error occurred: {}".format(e))
输出:
它成功运行并给出带有输入文本的语音输出,如下所示,
关于python - Azure TTS 超时 python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76891591/