【NetCore】.net core 文字转语音并实现语音播放

.net core 文字转语音并实现语音播放

引入包:System.Speech

通过NuGet程序包引用:System.Speech如果为.net core的项目引用:Unoffical.System.Speech程序包

引用:using System.Speech.Synthesis;

代码

csharp 复制代码
        /// <summary>
        /// 文字转换mp3格式音频
        /// </summary>
        /// <param name="path">保存路径</param>
        /// <param name="input">输入文本</param>
        /// <returns></returns>
        public static bool TextVonvertToMP3(string path, string input)
        {
            input = input.Trim();
            if (!string.IsNullOrWhiteSpace(input))
            {
                using (SpeechSynthesizer reader = new SpeechSynthesizer())
                {
                    reader.SetOutputToWaveFile(path + input + ".mp3");
                    reader.Speak(input);
                    reader.SetOutputToDefaultAudioDevice();
                    reader.Dispose();
                }
                return true;
            }
            return false;
        }
        /// <summary>
        /// 文字在线音频朗读
        /// </summary>
        /// <param name="readText">朗读文本</param>
        /// <returns></returns>
        public static bool TextRead(string readText)
        {
            var flag = false;
            readText = readText.Trim();
            if (!string.IsNullOrWhiteSpace(readText))
            {
                using (SpeechSynthesizer reader = new SpeechSynthesizer())
                {
                    reader.Speak(readText);
                    reader.Dispose();
                    flag = true;
                }
                return flag;
            }
            else
            {
                return flag;
            }
        }
相关推荐
hai3152475437 小时前
基于池化隔离的Linux内核原生hrtimer子系统的补充说明
人工智能
大黄说说7 小时前
码云数智门店系统赋能汽车服务门店全新发展
大数据·人工智能
lichong9517 小时前
让AI自己用电脑!Cua:后台操作鼠标键盘,Mac/Windows/Linux全支持
人工智能·macos·ai·计算机外设·agent·提示词
CH_Vaniteux7 小时前
自动驾驶调研-Day1
人工智能·机器学习·自动驾驶
DO_Community7 小时前
AI 创新先锋 Probably 携手 DigitalOcean 打造“本地优先”可验证智能体架构
人工智能·架构
生成论实验室7 小时前
自动驾驶:一个自主运动的系统
人工智能·算法·机器学习·语言模型·机器人·自动驾驶·安全架构
keykey6.7 小时前
集成学习:从 Bagging 到 XGBoost
人工智能·机器学习·集成学习
果丁智能7 小时前
从人工值守到云端智控:物联网智能锁重塑公寓与集团宿舍管理体系
大数据·人工智能·物联网·智能家居
Inhand陈工7 小时前
污水泵站PLC数据上云实战:西门子PLC + 映翰通IG502 + DM平台全流程
人工智能·物联网·网络安全·阿里云·信息与通信·iot
冷小鱼7 小时前
PyTorch 2.12 完全指南:从动态图到编译优化的深度学习框架演进
人工智能·pytorch·深度学习