这篇文章主要为大家详细介绍了C#实现文字转语音功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文分享C#实现文语转换的具体代码,供大家参考。具体内容如下
客户请求将文本内容转换为语音。因为内网环境,没有办法使用联网和在线。这是灵机一动。你能写一个简单的例子吗?搜索相关资料真的很好,不多说,有图有真相。
重点是,c#有现成的参考。
右键单击项目gt;添加参考gt;。Net gt找到系统。语音,然后单击确定。
控制台程序代码:
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Speech.Synthesis;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;???namespace TxtToVoice{? ? class Program? ? {? ? ? ? [STAThread] //默认线程模型是单线程单元 (STA) 模式? ? ? ? static void Main(string[] args)? ? ? ? {?? ? ? ? ? ? //Application.EnableVisualStyles();? ? ? ? ? ? //Application.SetCompatibleTextRenderingDefault(false);? ? ? ? ? ? //Application.Run(new Form1());?? ? ? ? ? ? //return;? ? ? ? ???? ? ? ? ? ? ? ?OpenFileDialog open = new OpenFileDialog();?? ? ? ? ? ? ? ?open.Title = "请选择文本"; //打开的文件选择对话框上的标题?? ? ? ? ? ? ? ?open.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";//设置文件类型?? ? ? ? ? ? ? ?open.InitialDirectory = @"D:\project\";//默认打开目录?? ? ? ? ? ? ? ?open.FilterIndex = 1;//设置默认文件类型显示顺序?? ? ? ? ? ? ? ?open.RestoreDirectory = false;//是否记忆上次打开的目录?? ? ? ? ? ? ? ?//open.Multiselect = true;//是否允许多选?? ? ? ? ? ? ? ?string content=string.Empty;?? ? ? ? ? ? ? ?if (open.ShowDialog() == DialogResult.OK)//按下确定选择的按钮? ? ? ? ? ? ? ?{?? ? ? ? ? ? ? ? ? ?string[] filename = open.FileNames;//获取多个文件的路径及文件名并存入数组?? ? ? ? ? ? ? ? ? ?MessageBox.Show(filename[0]);?? ? ? ? ? ? ? ? ? // MessageBox.Show(filename[1]);?? ? ? ? ? ? ? ? ? // MessageBox.Show(open.FileName); //获取路径及文件名?? ? ? ? ? ? ? ? ? // MessageBox.Show(open.SafeFileName);//获取文件名?? ? ? ? ? ? ? ? ? ?content = ReadFile(filename[0]);?? ? ? ? ? ? ? ?}? ? ? ? ? ? //-----------------------------------读出文件内容---------------------------------? ? ? ? ? ?? ? ? ? ? ? ? ?SpeechSynthesizer voice = new SpeechSynthesizer(); ? //创建语音实例? ? ? ? ? ? ? ?voice.Rate = -1; //设置语速,[-10,10]? ? ? ? ? ? ? ?voice.Volume = 100; //设置音量,[0,100]? ? ? ? ? ? ? ?//voice.SpeakAsync("Hellow Word"); ?//播放指定的字符串,这是异步朗读?? ? ? ? ? ? ? ?//下面的代码为一些SpeechSynthesizer的属性,看实际情况是否需要使用?? ? ? ? ? ? ? ?voice.SpeakAsyncCancelAll(); ?//取消朗读? ? ? ? ? ? ? ?voice.Speak(content); ?//同步朗读? ? ? ? ? ? ? ?voice.Pause(); ?//暂停朗读? ? ? ? ? ? ? ?voice.Resume(); //继续朗读?? ? ? ? ? ? ? ?voice.Dispose(); ?//释放所有语音资源?? ? ? ? }?? ? ? ? /// lt;summarygt;? ? ? ? /// 读取文件,返回相应字符串? ? ? ? /// lt;/summarygt;? ? ? ? /// lt;param name="fileName"gt;文件路径lt;/paramgt;? ? ? ? /// lt;returnsgt;返回文件内容lt;/returnsgt;? ? ? ? private static string ReadFile(string fileName)? ? ? ? {? ? ? ? ? ? StringBuilder str = new StringBuilder();? ? ? ? ? ? using (FileStream fs = File.OpenRead(fileName))? ? ? ? ? ? {? ? ? ? ? ? ? ? long left = fs.Length;? ? ? ? ? ? ? ? int maxLength = 100;//每次读取的最大长度? ? ? ? ? ? ? ? int start = 0;//起始位置? ? ? ? ? ? ? ? int num = 0;//已读取长度? ? ? ? ? ? ? ? while (left gt; 0)? ? ? ? ? ? ? ? {? ? ? ? ? ? ? ? ? ? byte[] buffer = new byte[maxLength];//缓存读取结果? ? ? ? ? ? ? ? ? ? char[] cbuffer = new char[maxLength];? ? ? ? ? ? ? ? ? ? fs.Position = start;//读取开始的位置? ? ? ? ? ? ? ? ? ? num = 0;? ? ? ? ? ? ? ? ? ? if (left lt; maxLength)? ? ? ? ? ? ? ? ? ? {? ? ? ? ? ? ? ? ? ? ? ? num = fs.Read(buffer, 0, Convert.ToInt32(left));? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? ? ? else? ? ? ? ? ? ? ? ? ? {? ? ? ? ? ? ? ? ? ? ? ? num = fs.Read(buffer, 0, maxLength);? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? ? ? if (num == 0)? ? ? ? ? ? ? ? ? ? {? ? ? ? ? ? ? ? ? ? ? ? break;? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? ? ? start += num;? ? ? ? ? ? ? ? ? ? left -= num;? ? ? ? ? ? ? ? ? ? str = str.Append(Encoding.UTF8.GetString(buffer));? ? ? ? ? ? ? ? }? ? ? ? ? ? }? ? ? ? ? ? return str.ToString();? ? ? ? }? ? }}
表单代码:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.IO;using System.Linq;using System.Speech.Synthesis;using System.Text;using System.Threading;using System.Threading.Tasks;using System.Windows.Forms;?namespace TxtToVoiceForm{? ? public partial class Form2 : Form? ? {? ? ? ? private SpeechSynthesizer speech;?? ? ? ? /// lt;summarygt;? ? ? ? /// 音量? ? ? ? /// lt;/summarygt;? ? ? ? private int value = 100;? ? ? ? /// lt;summarygt;? ? ? ? /// 语速? ? ? ? /// lt;/summarygt;? ? ? ? private int rate;?? ? ? ? public Form2()? ? ? ? {? ? ? ? ? ? InitializeComponent();? ? ? ? ? ? ReadlocalFile();? ? ? ? ? ? comboBox1.SelectedIndex = 0;? ? ? ? }?? ? ? ? private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)? ? ? ? {? ? ? ? ? ? rate = Int32.Parse(comboBox1.Text);? ? ? ? }?? ? ? ? //private void 打开文件ToolStripMenuItem_Click(object sender, EventArgs e)? ? ? ? //{? ? ? ? // ? ?this.ReadlocalFile();?? ? ? ? //}?? ? ? ? /// lt;summarygt;? ? ? ? /// 读取本地文本文件的方法? ? ? ? /// lt;/summarygt;? ? ? ? private void ReadlocalFile()? ? ? ? {? ? ? ? ? ? var open = new OpenFileDialog();?? ? ? ? ? ? open.ShowDialog();?? ? ? ? ? ? //得到文件路径? ? ? ? ? ? string path = open.FileName;?? ? ? ? ? ? if (path.Trim().Length == 0)? ? ? ? ? ? {?? ? ? ? ? ? ? ? return;? ? ? ? ? ? }?? ? ? ? ? ? var os = new StreamReader(path, Encoding.UTF8);? ? ? ? ? ? string str = os.ReadToEnd();? ? ? ? ? ? textBox1.Text = str;? ? ? ? }? ? ? ? private void 清空内容ToolStripMenuItem_Click(object sender, EventArgs e)? ? ? ? {? ? ? ? ? ? textBox1.Text = "";? ? ? ? }?? ? ? ? private void button1_Click(object sender, EventArgs e)? ? ? ? {? ? ? ? ? ? string text = textBox1.Text;?? ? ? ? ? ? if (text.Trim().Length == 0)? ? ? ? ? ? {? ? ? ? ? ? ? ? MessageBox.Show("不能阅读空内容!", "错误提示");? ? ? ? ? ? ? ? return;? ? ? ? ? ? }?? ? ? ? ? ? if (button1.Text == "语音试听")? ? ? ? ? ? {?? ? ? ? ? ? ? ? speech = new SpeechSynthesizer();?? ? ? ? ? ? ? ? new Thread(Speak).Start();?? ? ? ? ? ? ? ? button1.Text = "停止试听";?? ? ? ? ? ? }? ? ? ? ? ? else if (button1.Text == "停止试听")? ? ? ? ? ? {?? ? ? ? ? ? ? ? speech.SpeakAsyncCancelAll();//停止阅读?? ? ? ? ? ? ? ? button1.Text = "语音试听";? ? ? ? ? ? }? ? ? ? }?? ? ? ? private void Speak()? ? ? ? {?? ? ? ? ? ? speech.Rate = rate;? ? ? ? ? ? //speech.SelectVoice("Microsoft Lili");//设置播音员(中文)? ? ? ? ? ? //speech.SelectVoice("Microsoft Anna"); //英文? ? ? ? ? ? speech.Volume = value;? ? ? ? ? ? speech.SpeakAsync(textBox1.Text);//语音阅读方法? ? ? ? ? ? speech.SpeakCompleted += speech_SpeakCompleted;//绑定事件? ? ? ? }?? ? ? ? /// lt;summarygt;? ? ? ? /// 语音阅读完成触发此事件? ? ? ? /// lt;/summarygt;? ? ? ? /// lt;param name="sender"gt;lt;/paramgt;? ? ? ? /// lt;param name="e"gt;lt;/paramgt;? ? ? ? void speech_SpeakCompleted(object sender, SpeakCompletedEventArgs e)? ? ? ? {? ? ? ? ? ? button1.Text = "语音试听";? ? ? ? }?? ? ? ? /// lt;summarygt;? ? ? ? /// 拖动进度条事件? ? ? ? /// lt;/summarygt;? ? ? ? /// lt;param name="sender"gt;lt;/paramgt;? ? ? ? /// lt;param name="e"gt;lt;/paramgt;? ? ? ? private void trackBar1_Scroll(object sender, EventArgs e)? ? ? ? {? ? ? ? ? ? //因为trackBar1的值为(0-10)之间而音量值为(0-100)所以要乘10;? ? ? ? ? ? value = trackBar1.Value * 10;? ? ? ? }?? ? ? ? private void button2_Click(object sender, EventArgs e)? ? ? ? {???? ? ? ? ? ? string text = textBox1.Text;?? ? ? ? ? ? if (text.Trim().Length == 0)? ? ? ? ? ? {? ? ? ? ? ? ? ? MessageBox.Show("空内容无法生成!", "错误提示");? ? ? ? ? ? ? ? return;? ? ? ? ? ? }?? ? ? ? ? ? this.SaveFile(text);?? ? ? ? }?? ? ? ? /// lt;summarygt;? ? ? ? /// 生成语音文件的方法? ? ? ? /// lt;/summarygt;? ? ? ? /// lt;param name="text"gt;lt;/paramgt;? ? ? ? private void SaveFile(string text)? ? ? ? {? ? ? ? ? ? speech = new SpeechSynthesizer();? ? ? ? ? ? var dialog = new SaveFileDialog();? ? ? ? ? ? dialog.Filter = "*.wav|*.wav|*.mp3|*.mp3";? ? ? ? ? ? dialog.ShowDialog();?? ? ? ? ? ? string path = dialog.FileName;? ? ? ? ? ? if (path.Trim().Length == 0)? ? ? ? ? ? {? ? ? ? ? ? ? ? return;? ? ? ? ? ? }? ? ? ? ? ? speech.SetOutputToWaveFile(path);? ? ? ? ? ? speech.Volume = value;? ? ? ? ? ? speech.Rate = rate;? ? ? ? ? ? speech.Speak(text);? ? ? ? ? ? speech.SetOutputToNull();? ? ? ? ? ? MessageBox.Show("生成成功!在" + path + "路径中!", "提示");?? ? ? ? }?? ? ? ? private void label1_Click(object sender, EventArgs e)? ? ? ? {?? ? ? ? }?? ? ? ? private void label3_Click(object sender, EventArgs e)? ? ? ? {? ? ? ? ? ? this.ReadlocalFile();? ? ? ? }???? ? }}
意外地了解C#丰富的特性,或者自己动手,并没有想象中那么难。希望能帮助到有需要的朋友!
这就是本文的全部内容。希望对大家的学习有帮助,也希望大家能支持一下搜源网。
精彩评论