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