C#文字转语音代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Speech.Synthesis;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TextToSpeech
{
    public partial class Form1 : Form
    {
        private SpeechSynthesizer speech;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            /*
            using (var synth = new SpeechSynthesizer())
            {
                synth.SelectVoiceByHints(VoiceGender.Male); // 选择男性发音人员
                synth.Volume = 50;// 设置音量大小
                synth.Rate = 0; // 设置速度变化值

                string textToConvert = "你好";
                synth.SpeakAsync(textToConvert); // 开始转换
            }
            */
            speech = new SpeechSynthesizer();
            speech.Rate = 0;
            //speech.SelectVoice("Microsoft Lili");//设置播音员(中文)
            //speech.SelectVoice("Microsoft Anna"); //英文
            speech.Volume = 50;
            speech.SpeakAsync(textBox1.Text);//语音阅读方法
        }

        private void button2_Click(object sender, EventArgs e)
        {
            speech = new SpeechSynthesizer();
            var dialog = new SaveFileDialog();
            dialog.Filter = "*.wav|*.wav|*.mp3|*.mp3";
            dialog.ShowDialog();

            string path = dialog.FileName;
            if (path.Trim().Length == 0)
            {
                return;
            }
            speech.SetOutputToWaveFile(path);
            speech.Volume = 50;
            speech.Rate = 0;
            speech.Speak(textBox1.Text);
            speech.SetOutputToNull();
            MessageBox.Show("生成成功!在" + path + "路径中!", "提示");
        }
    }
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 在C#中,你可以使用微软的Azure Cognitive Services中的Text-to-Speech API来实现文字转语音功能。你可以使用Microsoft.CognitiveServices.Speech命名空间中的SpeechSynthesizer类来实现这个功能。首先,你需要在Azure门户上创建一个Cognitive Services资源,并获取到订阅密钥和区域信息。然后,你可以使用以下代码示例来实现文字转语音: ```csharp using Microsoft.CognitiveServices.Speech; using System; class Program { static async Task Main() { // 设置订阅密钥和区域信息 var subscriptionKey = "YourSubscriptionKey"; var region = "YourRegion"; // 创建SpeechSynthesizer对象 using (var synthesizer = new SpeechSynthesizer(new SpeechConfig(subscriptionKey, region))) { // 设置语音属性 var voice = "YourVoice"; // 选择合适的发声角色 var speechConfig = SpeechConfig.FromSubscription(subscriptionKey, region); speechConfig.SpeechSynthesisVoiceName = voice; // 输入要转换的文字 Console.WriteLine("请输入要转换的文字:"); var text = Console.ReadLine(); // 开始转换 using (var result = await synthesizer.SpeakTextAsync(text)) { // 检查转换是否成功 if (result.Reason == ResultReason.SynthesizingAudioCompleted) { // 保存语音到文件 var filePath = "output.wav"; await result.AudioData.WriteToWaveFileAsync(filePath); Console.WriteLine($"语音已保存到文件: {filePath}"); } else if (result.Reason == ResultReason.Canceled) { var cancellation = SpeechSynthesisCancellationDetails.FromResult(result); Console.WriteLine($"转换被取消: {cancellation.Reason}"); } } } } } ``` 请注意,你需要将"YourSubscriptionKey"和"YourRegion"替换为你自己的订阅密钥和区域信息。你还可以根据需要选择合适的发声角色,将"YourVoice"替换为你想要使用的角色名称。运行代码后,输入要转换的文字,程序将会将文字转换为语音并保存到文件中。 #### 引用[.reference_title] - *1* [免费使用微软Azure进行文字转语音!分享三个方法给大家!](https://blog.csdn.net/qq_25856657/article/details/128168859)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [azure微软文字转语音工具​AzureTools​使用](https://blog.csdn.net/chenfang8712/article/details/126597552)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [免费使用微软Azure进行文字转语音的三种方法!最自然接近人声的机器配音技术。](https://blog.csdn.net/weizuer123/article/details/126324360)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值