C#制作简单的语音朗读软件(可输出语音文件)

 时间:2024-09-20 21:31:40

需要引用类库:System.Speech.dll导入空间:usingSystem.IO;usingSystem.Speech;usingSystem.Speech.Synthesis;源代码地址给大家,有兴趣的朋友可以研究下https://pan.baidu.com/s/1skHk5zv

C#制作简单的语音朗读软件(可输出语音文件)

工具/原料

VS2013

.NETFramework4

方法/步骤

1、需要引用类库:System.Speech.dll解决方案资源管理器下找到项目,右键引用,添加引用,搜索关键字"speech"可找到“System.Speech”

C#制作简单的语音朗读软件(可输出语音文件)

2、在窗体加载时发生的事件下添加代码:textBox2.Text=Environment.CurrentDirectory;button2.Enabled=false;button3.Enabled=false;

C#制作简单的语音朗读软件(可输出语音文件)

3、在“开始朗读”事件下添加代码:if(richTextB艺皱麾酪ox1.Text.Trim()=="媪青怍牙"){return;}//判断是否输出文件if(checkBox1.Checked==true){try{speech=newSpeechSynthesizer();//同步朗读文本//speech.Speak(richTextBox1.Text);//异步朗读文本speech.SpeakAsync(richTextBox1.Text);speech.Volume=(int)num_yl.Value;//设置音量speech.Rate=(int)num_speed.Value;//设置朗读速度//输出文件speech.SetOutputToWaveFile(CheckPathTruth(textBox2.Text.Trim()));//输出语言文件button3.Enabled=true;button2.Enabled=true;}catch(Exceptionex){MessageBox.Show(ex.Message);return;}}else{try{speech=newSpeechSynthesizer();speech.SpeakAsync(richTextBox1.Text);speech.Volume=(int)num_yl.Value;//音量speech.Rate=(int)num_speed.Value;//朗读速度button3.Enabled=true;button2.Enabled=true;}catch(Exceptionex){MessageBox.Show(ex.Message);return;}}

4、在“取消朗读”事件下添加代码://停止朗读speech.SpeakAsyncCancelAll();//释放资源!speech.Dispose();

5、在“停止朗读”事件下添加代码:if(button3.Text=="暂停"){speech.Pause();//暂停button3.Text="继续";}elseif(button3.Text=="继续"){speech.Resume();//继续button3.Text="暂停";}

6、在“...”按钮下添加代码:OpenFileDialogopen=newO圬桦孰礅penFileDialog();op髫潋啜缅en.InitialDirectory="c:\\";//设置初始目录open.Multiselect=false;//设置是否可以选择多个文件open.DefaultExt=".txt";//设置默认文件拓展名open.Filter="txt文本|*.txt|所以文件|*.*";open.ShowHelp=true;//是否显示帮助按钮//判断是否点击了取消或关闭按钮//如果不判断,会出现异常if(open.ShowDialog()!=DialogResult.Cancel){stringstr=open.FileName;textBox1.Text=str;}else{return;}//获取文件内容,放到richTextBox1里richTextBox1.Text=GetFileStreamOrReadToEnd(textBox1.Text.Trim());

7、自定义函数如下:///<summary>///判断文件路径是否正确///</summary>///<paramname="path"></param>///<returns>返回正确的文件路径</returns>publicstringCheckPathTruth(stringpath){if(!path.Contains(".wav")){MessageBox.Show("输出文件有误!");returnnull;}else{returnpath;}returnnull;}

8、自定义函数如下:///<summary>///获取文竭惮蚕斗件内容///&造婷用痃lt;/summary>///<paramname="filepath"></param>///<returns></returns>publicstringGetFileStreamOrReadToEnd(stringfilepath){FileStreamfs=newFileStream(filepath,FileMode.OpenOrCreate);StreamReadersr=newStreamReader(fs,Encoding.Default);//获取所以文本stringstr=sr.ReadToEnd();//要关闭!fs.Close();sr.Close();returnstr;}

java中怎样获取静态变量的值 matlab2010证书过期解决办法 hbuilderx怎么折叠其他的项目? Excel怎么制作随机点名小程序 解决无法打开64位操作系统的虚拟机问题
热门搜索
猫和老鼠图片大全 地砖图片 爱国手抄报图片 金针菜图片 奥特曼大全图片