




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第C#实现文字转语音功能本文实例为大家分享了C#实现文字转语音的具体代码,供大家参考,具体内容如下
客户提出要求,将文字内容转为语音,因为内网环境,没办法采用联网,在线这种方式,灵机一动,能否写一个简单的例子呢,搜索相关资料还真行,话不多说,有图有真相
关键是,c#有现成的一个引用
右键点击项目添加引用.Net找到System.Speech点击确定
控制台程序代码:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.IO;
usingSystem.Linq;
usingSystem.Speech.Synthesis;
usingSystem.Text;
usingSystem.Threading.Tasks;
usingSystem.Windows.Forms;
namespaceTxtToVoice
classProgram
{
[STAThread]//默认线程模型是单线程单元(STA)模式
staticvoidMain(string[]args)
{
//Application.EnableVisualStyles();
//Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(newForm1());
//return;
OpenFileDialogopen=newOpenFileDialog();
open.Title="请选择文本";//打开的文件选择对话框上的标题
open.Filter="文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";//设置文件类型
open.InitialDirectory=@"D:\project\";//默认打开目录
open.FilterIndex=1;//设置默认文件类型显示顺序
open.RestoreDirectory=false;//是否记忆上次打开的目录
//open.Multiselect=true;//是否允许多选
stringcontent=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]);
}
//-----------------------------------读出文件内容---------------------------------
SpeechSynthesizervoice=newSpeechSynthesizer();
//创建语音实例
voice.Rate=-1;//设置语速,[-10,10]
voice.Volume=100;//设置音量,[0,100]
//voice.SpeakAsync("HellowWord");
//播放指定的字符串,这是异步朗读
//下面的代码为一些SpeechSynthesizer的属性,看实际情况是否需要使用
voice.SpeakAsyncCancelAll();
//取消朗读
voice.Speak(content);
//同步朗读
voice.Pause();
//暂停朗读
voice.Resume();//继续朗读
voice.Dispose();
//释放所有语音资源
}
///summary
///读取文件,返回相应字符串
////summary
///paramname="fileName"文件路径/param
///returns返回文件内容/returns
privatestaticstringReadFile(stringfileName)
{
StringBuilderstr=newStringBuilder();
using(FileStreamfs=File.OpenRead(fileName))
{
longleft=fs.Length;
intmaxLength=100;//每次读取的最大长度
intstart=0;//起始位置
intnum=0;//已读取长度
while(left0)
{
byte[]buffer=newbyte[maxLength];//缓存读取结果
char[]cbuffer=newchar[maxLength];
fs.Position=start;//读取开始的位置
num=0;
if(leftmaxLength)
{
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));
}
}
returnstr.ToString();
}
}
}
窗体代码:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.IO;
usingSystem.Linq;
usingSystem.Speech.Synthesis;
usingSystem.Text;
usingSystem.Threading;
usingSystem.Threading.Tasks;
usingSystem.Windows.Forms;
namespaceTxtToVoiceForm
publicpartialclassForm2:Form
{
privateSpeechSynthesizerspeech;
///summary
///音量
////summary
privateintvalue=100;
///summary
///语速
////summary
privateintrate;
publicForm2()
{
InitializeComponent();
ReadlocalFile();
comboBox1.SelectedIndex=0;
}
privatevoidcomboBox1_SelectedIndexChanged(objectsender,EventArgse)
{
rate=Int32.Parse(comboBox1.Text);
}
//privatevoid打开文件ToolStripMenuItem_Click(objectsender,EventArgse)
//{
//
this.ReadlocalFile();
//}
///summary
///读取本地文本文件的方法
////summary
privatevoidReadlocalFile()
{
varopen=newOpenFileDialog();
open.ShowDialog();
//得到文件路径
stringpath=open.FileName;
if(path.Trim().Length==0)
{
return;
}
varos=newStreamReader(path,Encoding.UTF8);
stringstr=os.ReadToEnd();
textBox1.Text=str;
}
privatevoid清空内容ToolStripMenuItem_Click(objectsender,EventArgse)
{
textBox1.Text="";
}
privatevoidbutton1_Click(objectsender,EventArgse)
{
stringtext=textBox1.Text;
if(text.Trim().Length==0)
{
MessageBox.Show("不能阅读空内容!","错误提示");
return;
}
if(button1.Text=="语音试听")
{
speech=newSpeechSynthesizer();
newThread(Speak).Start();
button1.Text="停止试听";
}
elseif(button1.Text=="停止试听")
{
speech.SpeakAsyncCancelAll();//停止阅读
button1.Text="语音试听";
}
}
privatevoidSpeak()
{
speech.Rate=rate;
//speech.SelectVoice("MicrosoftLili");//设置播音员(中文)
//speech.SelectVoice("MicrosoftAnna");//英文
speech.Volume=value;
speech.SpeakAsync(textBox1.Text);//语音阅读方法
speech.SpeakCompleted+=speech_SpeakCompleted;//绑定事件
}
///summary
///语音阅读完成触发此事件
////summary
///paramname="sender"/param
///paramname="e"/param
voidspeech_SpeakCompleted(objectsender,SpeakCompletedEventArgse)
{
button1.Text="语音试听";
}
///summary
///拖动进度条事件
////summary
///paramname="sender"/param
///paramname="e"/param
privatevoidtrackBar1_Scroll(objectsender,EventArgse)
{
//因为trackBar1的值为(0-10)之间而音量值为(0-100)所以要乘10;
value=trackBar1.Value*10;
}
privatevoidbutton2_Click(objectsender,EventArgse)
{
stringtext=textBox1.Text;
if(text.Trim().Length==0)
{
MessageBox.Show("空内容无法生成!","错误提示");
return;
}
this.SaveFile(text);
}
///summary
///生成语音文件的方法
////summary
///paramname="text"/param
privatevoidSaveFile(stringtext)
{
speech=newSpeechSynthesizer()
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 提升义务教育跨学科教学效果的有效策略与实践路径
- 网络安全事件处理流程试题及答案
- 2023-2025北京高三二模语文汇编:词语
- 2025年公司数字化战略探索试题及答案
- 信息处理技术员复习计划与试题答案
- 2025年小学老师师德自查自纠总结模版
- 电力设备行业发展趋势与市场展望分析
- 初中生物跨学科教学的创新策略与实践路径
- 行政法学的创新思维试题与答案
- 社会实践主题演讲讲话发言稿参考范文范文(4篇)
- 2025四川爱众集团第一批次招聘10人笔试参考题库附带答案详解
- 工业用地开发项目成本分析与资金筹措方案
- 2025年初中地理学业水平考试模拟试卷:地图与地球知识综合训练试题卷及答案
- (人教2024版)英语七年级下册Unit7.4 Section B 1a-2d课件(新教材)
- 2025年湖北荆州市监利市畅惠交通投资有限公司招聘笔试参考题库含答案解析
- 酒店入股合同协议书
- 银行sql考试题及答案
- 隔离技术知识试题及答案
- 2025三方贸易协议合同范本 贸易合同范本
- 2025-2030中国聚苯醚行业市场发展趋势与前景展望战略研究报告
- 山东省临沂市2025年普通高等学校招生全国统一考试(模拟)历史及答案(临沂二模)
评论
0/150
提交评论