.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程序包 親測不支持跨平台


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM