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


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

        /// <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;
            }
        }
Unoffical.System.Speech程序包 亲测不支持跨平台

原文地址:https://www.cnblogs.com/s666/p/15021548.html