【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;
            }
        }
相关推荐
搏博5 分钟前
抗量子计算攻击的数据安全体系构建:从理论突破到工程实践
人工智能·人机交互·量子计算
白熊18810 分钟前
【计算机视觉】OpenCV实战项目:GraspPicture 项目深度解析:基于图像分割的抓取点检测系统
人工智能·opencv·计算机视觉
python15622 分钟前
OpenWebUI新突破,MCPO框架解锁MCP工具新玩法
人工智能·语言模型·自然语言处理
墨绿色的摆渡人35 分钟前
pytorch小记(二十一):PyTorch 中的 torch.randn 全面指南
人工智能·pytorch·python
东临碣石821 小时前
【AI论文】EnerVerse-AC:用行动条件来构想具身环境
人工智能
lqjun08272 小时前
PyTorch实现CrossEntropyLoss示例
人工智能·pytorch·python
心灵彼岸-诗和远方2 小时前
芯片生态链深度解析(三):芯片设计篇——数字文明的造物主战争
人工智能·制造
小蜗笔记2 小时前
显卡、Cuda和pytorch兼容问题
人工智能·pytorch·python
矿工学编程2 小时前
.NET Core liunx二进制文件安装
.netcore
高建伟-joe2 小时前
内容安全:使用开源框架Caffe实现上传图片进行敏感内容识别
人工智能·python·深度学习·flask·开源·html5·caffe