




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
第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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 《稅收合規性補充申報智能管理系統》介紹課件
- 計算機一級試題及參考答案解析
- 5月消防設施操作員試題(含參考答案)解析
- 毛織造企業組織結構與人力資源管理考核試卷
- 班主任家長會課件下載
- 紙容器生產過程中的能耗監測與優化考核試卷
- 2025年印刷品、記錄媒介復制品項目合作計劃書
- 空調器智能濕度控制系統考核試卷
- 《秋天的落葉》課件
- 網絡安全防護在智慧城市的智能垃圾分類系統中評估考核試卷
- 2025年湖北荊州市監利市暢惠交通投資有限公司招聘筆試參考題庫含答案解析
- 酒店入股合同協議書
- 2025-2030中國無煙原煤行業市場現狀供需分析及市場深度研究發展前景及規劃可行性分析研究報告
- GB/T 32960.3-2025電動汽車遠程服務與管理系統技術規范第3部分:通信協議及數據格式
- 2024年江蘇省勞動關系研究院招聘考試真題
- 2024年四川省公安廳招聘警務輔助人員真題
- 突發性聾診療指南(2025版)
- 2025年電子信息工程師職業資格考試試卷及答案
- 糧食局業務知識課件
- 全套教學課件《工程倫理學》
- 江蘇省建筑與裝飾工程計價定額(2014)電子表格版
評論
0/150
提交評論