




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
第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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 八年級物理(上冊)練習試題
- 小學生辯論賽課件
- 小學生課件兒歌視頻教學
- 10 必修1 第二單元 第9講 細胞呼吸的原理和應用
- 跨國項目數據合規審查與隱私保護服務協議
- 跨境電商采購合同糾紛解決與合規管理
- 文化場館安保服務勞務派遣合同
- 醫學常見病癥診斷與處理知識試卷含法洛四聯征等案例
- 2024-2025學年湖北省云學聯盟高一下學期5月月考歷史試題及答案
- 團隊管理實踐案例分析框架構建考核試卷
- 電工廠搬遷方案(3篇)
- 2025年南京市中考數學真題試卷
- 老年人眼科疾病
- 鋼板配送設計方案(3篇)
- 2025年內蒙古煤炭地質勘查(集團)一零九有限公司招聘筆試參考題庫含答案解析
- 中醫基礎學課件護理情志
- 小學三年級科學下冊教案
- T/CBMCA 039-2023陶瓷大板巖板裝修鑲貼應用規范
- 2025-2030中國美容美發行業市場現狀供需分析及投資評估規劃分析研究報告
- 2025年中國不銹鋼蝕刻板數據監測研究報告
- 免疫檢查點抑制劑相關肺炎診治和管理專家共識(2025)要點解讀
評論
0/150
提交評論