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博客

相关推荐
杜子不疼.7 小时前
【AI】基于GLM-4_7与数字人SDK的政务大厅智能指引系统实践
人工智能·microsoft·政务
kaka.liulin -study11 小时前
EmberTrace AI | Multimodel Map GIS: 基于多智能体协作的地图自主化分析技术
人工智能·microsoft
小陈phd13 小时前
langGraph从入门到精通(四)——基于LangGraph的State状态模式设计
python·microsoft·状态模式
viperrrrrrrrrr713 小时前
DeepSeek-V4架构详解
microsoft·deepseek-v4
拓端研究室16 小时前
2026年人形机器人展望报告:市场趋势、技术创新与行业应用|附300+份报告PDF、数据、可视化模板汇总下载
大数据·人工智能·microsoft
2501_9419820516 小时前
企业微信外部群自动化消息推送实战
microsoft·自动化·企业微信
摘星编程18 小时前
React Native for OpenHarmony 实战:Animated 动画组件详解
flutter·microsoft
洛水如云1 天前
重塑数据管理逻辑!文件夹同步的实用指南(含工具选型 + 实操步骤)
windows·microsoft·电脑
洛水如云2 天前
笔记本数据迁移新电脑:从备份到实操的高效指南(适用于台式机)
windows·microsoft·电脑
天使奇迹2 天前
2026数字人平台选型指南:五大技术路线解析与主流厂商评估
microsoft