C#實(shí)現(xiàn)文字轉(zhuǎn)語(yǔ)音功能_第1頁(yè)
C#實(shí)現(xiàn)文字轉(zhuǎn)語(yǔ)音功能_第2頁(yè)
C#實(shí)現(xiàn)文字轉(zhuǎn)語(yǔ)音功能_第3頁(yè)
C#實(shí)現(xiàn)文字轉(zhuǎn)語(yǔ)音功能_第4頁(yè)
C#實(shí)現(xiàn)文字轉(zhuǎn)語(yǔ)音功能_第5頁(yè)
已閱讀5頁(yè),還剩5頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第C#實(shí)現(xiàn)文字轉(zhuǎn)語(yǔ)音功能本文實(shí)例為大家分享了C#實(shí)現(xiàn)文字轉(zhuǎn)語(yǔ)音的具體代碼,供大家參考,具體內(nèi)容如下

客戶(hù)提出要求,將文字內(nèi)容轉(zhuǎn)為語(yǔ)音,因?yàn)閮?nèi)網(wǎng)環(huán)境,沒(méi)辦法采用聯(lián)網(wǎng),在線(xiàn)這種方式,靈機(jī)一動(dòng),能否寫(xiě)一個(gè)簡(jiǎn)單的例子呢,搜索相關(guān)資料還真行,話(huà)不多說(shuō),有圖有真相

關(guān)鍵是,c#有現(xiàn)成的一個(gè)引用

右鍵點(diǎn)擊項(xiàng)目添加引用.Net找到System.Speech點(diǎn)擊確定

控制臺(tái)程序代碼:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.IO;

usingSystem.Linq;

usingSystem.Speech.Synthesis;

usingSystem.Text;

usingSystem.Threading.Tasks;

usingSystem.Windows.Forms;

namespaceTxtToVoice

classProgram

{

[STAThread]//默認(rèn)線(xiàn)程模型是單線(xiàn)程單元(STA)模式

staticvoidMain(string[]args)

{

//Application.EnableVisualStyles();

//Application.SetCompatibleTextRenderingDefault(false);

//Application.Run(newForm1());

//return;

OpenFileDialogopen=newOpenFileDialog();

open.Title="請(qǐng)選擇文本";//打開(kāi)的文件選擇對(duì)話(huà)框上的標(biāo)題

open.Filter="文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";//設(shè)置文件類(lèi)型

open.InitialDirectory=@"D:\project\";//默認(rèn)打開(kāi)目錄

open.FilterIndex=1;//設(shè)置默認(rèn)文件類(lèi)型顯示順序

open.RestoreDirectory=false;//是否記憶上次打開(kāi)的目錄

//open.Multiselect=true;//是否允許多選

stringcontent=string.Empty;

if(open.ShowDialog()==DialogResult.OK)//按下確定選擇的按鈕

{

string[]filename=open.FileNames;//獲取多個(gè)文件的路徑及文件名并存入數(shù)組

MessageBox.Show(filename[0]);

//MessageBox.Show(filename[1]);

//MessageBox.Show(open.FileName);//獲取路徑及文件名

//MessageBox.Show(open.SafeFileName);//獲取文件名

content=ReadFile(filename[0]);

}

//-----------------------------------讀出文件內(nèi)容---------------------------------

SpeechSynthesizervoice=newSpeechSynthesizer();

//創(chuàng)建語(yǔ)音實(shí)例

voice.Rate=-1;//設(shè)置語(yǔ)速,[-10,10]

voice.Volume=100;//設(shè)置音量,[0,100]

//voice.SpeakAsync("HellowWord");

//播放指定的字符串,這是異步朗讀

//下面的代碼為一些SpeechSynthesizer的屬性,看實(shí)際情況是否需要使用

voice.SpeakAsyncCancelAll();

//取消朗讀

voice.Speak(content);

//同步朗讀

voice.Pause();

//暫停朗讀

voice.Resume();//繼續(xù)朗讀

voice.Dispose();

//釋放所有語(yǔ)音資源

}

///summary

///讀取文件,返回相應(yīng)字符串

////summary

///paramname="fileName"文件路徑/param

///returns返回文件內(nèi)容/returns

privatestaticstringReadFile(stringfileName)

{

StringBuilderstr=newStringBuilder();

using(FileStreamfs=File.OpenRead(fileName))

{

longleft=fs.Length;

intmaxLength=100;//每次讀取的最大長(zhǎng)度

intstart=0;//起始位置

intnum=0;//已讀取長(zhǎng)度

while(left0)

{

byte[]buffer=newbyte[maxLength];//緩存讀取結(jié)果

char[]cbuffer=newchar[maxLength];

fs.Position=start;//讀取開(kāi)始的位置

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

///語(yǔ)速

////summary

privateintrate;

publicForm2()

{

InitializeComponent();

ReadlocalFile();

comboBox1.SelectedIndex=0;

}

privatevoidcomboBox1_SelectedIndexChanged(objectsender,EventArgse)

{

rate=Int32.Parse(comboBox1.Text);

}

//privatevoid打開(kāi)文件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清空內(nèi)容ToolStripMenuItem_Click(objectsender,EventArgse)

{

textBox1.Text="";

}

privatevoidbutton1_Click(objectsender,EventArgse)

{

stringtext=textBox1.Text;

if(text.Trim().Length==0)

{

MessageBox.Show("不能閱讀空內(nèi)容!","錯(cuò)誤提示");

return;

}

if(button1.Text=="語(yǔ)音試聽(tīng)")

{

speech=newSpeechSynthesizer();

newThread(Speak).Start();

button1.Text="停止試聽(tīng)";

}

elseif(button1.Text=="停止試聽(tīng)")

{

speech.SpeakAsyncCancelAll();//停止閱讀

button1.Text="語(yǔ)音試聽(tīng)";

}

}

privatevoidSpeak()

{

speech.Rate=rate;

//speech.SelectVoice("MicrosoftLili");//設(shè)置播音員(中文)

//speech.SelectVoice("MicrosoftAnna");//英文

speech.Volume=value;

speech.SpeakAsync(textBox1.Text);//語(yǔ)音閱讀方法

speech.SpeakCompleted+=speech_SpeakCompleted;//綁定事件

}

///summary

///語(yǔ)音閱讀完成觸發(fā)此事件

////summary

///paramname="sender"/param

///paramname="e"/param

voidspeech_SpeakCompleted(objectsender,SpeakCompletedEventArgse)

{

button1.Text="語(yǔ)音試聽(tīng)";

}

///summary

///拖動(dòng)進(jìn)度條事件

////summary

///paramname="sender"/param

///paramname="e"/param

privatevoidtrackBar1_Scroll(objectsender,EventArgse)

{

//因?yàn)閠rackBar1的值為(0-10)之間而音量值為(0-100)所以要乘10;

value=trackBar1.Value*10;

}

privatevoidbutton2_Click(objectsender,EventArgse)

{

stringtext=textBox1.Text;

if(text.Trim().Length==0)

{

MessageBox.Show("空內(nèi)容無(wú)法生成!","錯(cuò)誤提示");

return;

}

this.SaveFile(text);

}

///summary

///生成語(yǔ)音文件的方法

////summary

///paramname="text"/param

privatevoidSaveFile(stringtext)

{

speech=newSpeechSynthesizer()

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論