【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;
            }
        }
相关推荐
Easy_API6 小时前
2.8万亿参数,编程反超GPT-6 Astra:Kimi K3.1 Pro把开源模型推到了哪一步
大数据·人工智能·gpt·深度学习
GlobalInfo6 小时前
2026年机器人多指灵巧手市场分析报告——涵盖行业调查、趋势分析与发展前景
大数据·人工智能·microsoft·微服务·机器人
紧跟先前的步伐6 小时前
Go AI Agent 实战 24|工具系统工程化收尾与阶段验收
人工智能·ai·golang·1024程序员节
释厄6237 小时前
AI学习层更新与跨体转移律——人类社会同构·不缺理解缺 U
人工智能·学习
老金带你玩AI8 小时前
集万家所长,一键复制的Agent.md!
人工智能
ZhuoAIStage8 小时前
舞台工艺多专业审图提速:用 AI 智能体编排 A+B+C+G 四段读图流水线
人工智能·自动化·工作流编排·舞台工艺·审图智能体
小奶包他干奶奶8 小时前
AI学习——1、机器学习概念
人工智能·机器学习
展讯快报8 小时前
悬浮提词器免费App评估报告:说得AI的免费策略与可用性分析
人工智能
Yuzhiyuxia8 小时前
【AI Agent面试系列】AI Agent记忆机制知识点1
人工智能·面试·职场和发展
2601_965448878 小时前
AI视频制作:做系列历史人物视频音色不搭,AI 音色克隆的方法实测
人工智能·音视频