c语言实现将文本转换为语音,C#文字转换语音朗读或保存MP3、WAV等格式
最近遇到一個(gè)需求,需要把文字轉(zhuǎn)換語(yǔ)音,參考很多大佬寫的方法,最后經(jīng)過自己改造實(shí)現(xiàn)文字在線朗讀、保存MP3、WAV等格式。
//需要引用System.Speech程序集
//引用using System.Speech.Synthesis;
在線朗讀代碼:
///
/// 文字在線音頻朗讀
///
/// 朗讀文本
///
public static bool TextRead(string readText)
{
var flag = false;
if (!string.IsNullOrWhiteSpace(readText))
{
using (SpeechSynthesizer reader = new SpeechSynthesizer())
{
reader.SpeakAsync(readText);
reader.Dispose();
flag = true;
}
return flag;
}
else
{
return flag;
}
}
保存MP3、WAV等格式:
///
/// 文字轉(zhuǎn)換mp3格式音頻
///
/// 保存路徑
/// 輸入文本
///
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;
}
注:忘記了參考文章出處,請(qǐng)各位大佬見諒!!!
總結(jié)
以上是生活随笔為你收集整理的c语言实现将文本转换为语音,C#文字转换语音朗读或保存MP3、WAV等格式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springboot, thymelea
- 下一篇: c#设计一个方法,与使用