Azure 将文本转换为语音

注意:只需要使用 speech 语音服务,不用Azure OpenAi

环境变量

复制代码
setx SPEECH_KEY your-key
setx SPEECH_REGION your-region

需要安装库

复制代码
pip install azure-cognitiveservices-speech

import os
import azure.cognitiveservices.speech as speechsdk

# This example requires environment variables named "SPEECH_KEY" and "SPEECH_REGION"
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)

# The neural multilingual voice can speak different languages based on the input text.
speech_config.speech_synthesis_voice_name='en-US-AvaMultilingualNeural'

speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config)

# Get text from the console and synthesize to the default speaker.
print("Enter some 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 the speech resource key and region values?")

参见:

文本转语音快速入门 - 语音服务 - Azure AI services | Microsoft Learn

另一篇:

Azure OpenAI 语音转语音聊天_azure openai 语音转语音聊天 restful-CSDN博客

相关推荐
海盗12345 小时前
微软技术日报 2026-09-10:Project Zenith 开发者设备亮相,.NET 11 RC1 可投产
microsoft·r语言·.net
小白学大数据8 小时前
Codex 里的 GPT 6 Astra、GPT 5.6 Sol、Terra、Luna 怎么选
开发语言·gpt·microsoft
YCOSA202518 小时前
系统工具使用检测.exe (仅供娱乐)
python·microsoft
ly76891 天前
Spring WebFlux背压机制在生产环境的调优与陷阱
java·spring·microsoft·webflux·project reactor·背压·响应式流
ituff1 天前
ShareOneList:一个应用,串起三种 Microsoft 365 账户
microsoft·sharepoint·shareonelist
云雀衔光1 天前
MCP + 应用生成:让 AI 直接产出可交互的应用
java·人工智能·测试工具·microsoft·交互·ai编程
聪明蛋子哟1 天前
教大模型“算概率”:从贝叶斯推理看LLM Agent如何实现不确定性决策
microsoft
极小狐1 天前
干掉流水线里的长期密钥:用 OIDC 让 CI/CD 与云以临时凭证对接
microsoft·ci/cd·gitlab·devops·ci·cd·oidc
richard_first1 天前
50.5% 美国人认为 AI 恋爱可能算出轨:AI 正在从“工具“变成“关系主体“
人工智能·microsoft
天空属于哈夫克32 天前
企业微信AI开发:如何让AI根据用户消息自动生成回复?
人工智能·microsoft