惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 【当耐特】
Microsoft Azure Blog
Microsoft Azure Blog
Google DeepMind News
Google DeepMind News
Apple Machine Learning Research
Apple Machine Learning Research
有赞技术团队
有赞技术团队
Y
Y Combinator Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
爱范儿
爱范儿
L
LangChain Blog
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
MongoDB | Blog
MongoDB | Blog
Hugging Face - Blog
Hugging Face - Blog
G
Google Developers Blog
T
Tailwind CSS Blog
Engineering at Meta
Engineering at Meta
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
宝玉的分享
宝玉的分享
博客园 - 三生石上(FineUI控件)
D
DataBreaches.Net
Recent Announcements
Recent Announcements
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - WmW

C#学习牛顿迭代法开方 mysql将一个表中指定时间之后的新数据导入到另一个表中 使用具体时间和DateTime.Now运算时需要注意DateTime.Now的毫秒数 简单搭建一个 ASP.NET Core Web API + Mysql + SqlSugar demo项目 C# 学习研究CRC校验 C# 学习逆变和协变 C# 标准的Dispose模式 C# 返回Task或者Task<T>的方法中如果没有异步方法,就没必要使用async修饰 关于字节序的概念加深 C# 基于ReadOnlySequence和ReadOnlySequenceSegment的简单封装 简单接触BCD码,以及使用C#简单实现BCD转换 C# ReadOnlySequence和ReadOnlySequenceSegment简单使用 C# 输出年龄和属相列表 C# 封装了一个用来对参数值进行范围限制的泛型方法 C# 为WindowsDefender防火墙已经存在的入站规则添加IP地址 C# 将Framework4.8控制台程序注册为windows服务 C# async void 方法中使用await时外部不会等待 C# Channel学习 C# 使用字符串分割字符串 C# 一个简单的连续心率血氧压缩算法 C# 将日期时间按照ISO 8601标准转成字符串 Dapper传递参数对象时,只支持属性,无法解析字段(出现Parameter '?id' must be defined)
C# 非常简单的文字转语音实现
WmW · 2025-03-27 · via 博客园 - WmW

之前废了老大劲实现了文本转语音功能,结果发现C#官方有个非常简单的实现,泪流满面,应该是用不上了,记录一下方便后来人吧

感谢 https://zhuanlan.zhihu.com/p/32960822565

需要先在nuget中引用System.Speech包,然后就是下面的使用方式

        public void Test() {
            string text = "测试123abc_@xyz";
            //Speak(text);
            GenerateAudioFile(text, "D:\\text.wav");
        }
        /// <summary>
        /// 播放语音
        /// </summary> 
        static void Speak(string text) {
            using (SpeechSynthesizer synthesizer = new SpeechSynthesizer()) {
                synthesizer.Speak(text);
            }
        }
        /// <summary>
        /// 生成语音文件
        /// </summary> 
        static void GenerateAudioFile(string text, string path) {
            using (SpeechSynthesizer synthesizer = new SpeechSynthesizer()) {
                using (MemoryStream stream = new MemoryStream()) {
                    synthesizer.SetOutputToWaveStream(stream); // 设置语音合成的输出目标为内存流 
                    synthesizer.Speak(text);
                    using (FileStream fileStream = new FileStream(path, FileMode.Create)) {
                        stream.Seek(0, SeekOrigin.Begin);
                        stream.CopyTo(fileStream);
                    }
                }
            }
        }