C#實(shí)現(xiàn)文字轉(zhuǎn)語音功能
本文實(shí)例為大家分享了C#實(shí)現(xiàn)文字轉(zhuǎn)語音的具體代碼,供大家參考,具體內(nèi)容如下
客戶提出要求,將文字內(nèi)容轉(zhuǎn)為語音,因?yàn)閮?nèi)網(wǎng)環(huán)境,沒辦法采用聯(lián)網(wǎng),在線這種方式,靈機(jī)一動,能否寫一個(gè)簡單的例子呢,搜索相關(guān)資料還真行,話不多說,有圖有真相
關(guān)鍵是,c#有現(xiàn)成的一個(gè)引用
右鍵點(diǎn)擊項(xiàng)目 > 添加引用 > .Net > 找到System.Speech點(diǎn)擊確定
控制臺程序代碼:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Speech.Synthesis; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; ? ? ? namespace TxtToVoice { ? ? class Program ? ? { ? ? ? ? [STAThread] //默認(rèn)線程模型是單線程單元 (STA) 模式 ? ? ? ? static void Main(string[] args) ? ? ? ? { ? ? ? ? ? ? ? //Application.EnableVisualStyles(); ? ? ? ? ? ? //Application.SetCompatibleTextRenderingDefault(false); ? ? ? ? ? ? //Application.Run(new Form1()); ? ? ? ? ? ? ? //return; ? ? ? ? ?? ? ? ? ? ? ? ? ? ?OpenFileDialog open = new OpenFileDialog(); ? ? ? ? ? ? ? ? ?open.Title = "請選擇文本"; //打開的文件選擇對話框上的標(biāo)題 ? ? ? ? ? ? ? ? ?open.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";//設(shè)置文件類型 ? ? ? ? ? ? ? ? ?open.InitialDirectory = @"D:\project\";//默認(rèn)打開目錄 ? ? ? ? ? ? ? ? ?open.FilterIndex = 1;//設(shè)置默認(rèn)文件類型顯示順序 ? ? ? ? ? ? ? ? ?open.RestoreDirectory = false;//是否記憶上次打開的目錄 ? ? ? ? ? ? ? ? ?//open.Multiselect = true;//是否允許多選 ? ? ? ? ? ? ? ? ?string content=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)容--------------------------------- ? ? ? ? ? ? ? ? ? ? ? ? ? ?SpeechSynthesizer voice = new SpeechSynthesizer(); ? //創(chuàng)建語音實(shí)例 ? ? ? ? ? ? ? ?voice.Rate = -1; //設(shè)置語速,[-10,10] ? ? ? ? ? ? ? ?voice.Volume = 100; //設(shè)置音量,[0,100] ? ? ? ? ? ? ? ?//voice.SpeakAsync("Hellow Word"); ?//播放指定的字符串,這是異步朗讀 ? ? ? ? ? ? ? ? ?//下面的代碼為一些SpeechSynthesizer的屬性,看實(shí)際情況是否需要使用 ? ? ? ? ? ? ? ? ?voice.SpeakAsyncCancelAll(); ?//取消朗讀 ? ? ? ? ? ? ? ?voice.Speak(content); ?//同步朗讀 ? ? ? ? ? ? ? ?voice.Pause(); ?//暫停朗讀 ? ? ? ? ? ? ? ?voice.Resume(); //繼續(xù)朗讀 ? ? ? ? ? ? ? ? ?voice.Dispose(); ?//釋放所有語音資源 ? ? ? ? ? } ? ? ? ? ? /// <summary> ? ? ? ? /// 讀取文件,返回相應(yīng)字符串 ? ? ? ? /// </summary> ? ? ? ? /// <param name="fileName">文件路徑</param> ? ? ? ? /// <returns>返回文件內(nèi)容</returns> ? ? ? ? private static string ReadFile(string fileName) ? ? ? ? { ? ? ? ? ? ? StringBuilder str = new StringBuilder(); ? ? ? ? ? ? using (FileStream fs = File.OpenRead(fileName)) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? long left = fs.Length; ? ? ? ? ? ? ? ? int maxLength = 100;//每次讀取的最大長度 ? ? ? ? ? ? ? ? int start = 0;//起始位置 ? ? ? ? ? ? ? ? int num = 0;//已讀取長度 ? ? ? ? ? ? ? ? while (left > 0) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? byte[] buffer = new byte[maxLength];//緩存讀取結(jié)果 ? ? ? ? ? ? ? ? ? ? char[] cbuffer = new char[maxLength]; ? ? ? ? ? ? ? ? ? ? fs.Position = start;//讀取開始的位置 ? ? ? ? ? ? ? ? ? ? num = 0; ? ? ? ? ? ? ? ? ? ? if (left < maxLength) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? 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)); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? return str.ToString(); ? ? ? ? } ? ? } }
窗體代碼:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Speech.Synthesis; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; ? namespace TxtToVoiceForm { ? ? public partial class Form2 : Form ? ? { ? ? ? ? private SpeechSynthesizer speech; ? ? ? ? ? /// <summary> ? ? ? ? /// 音量 ? ? ? ? /// </summary> ? ? ? ? private int value = 100; ? ? ? ? /// <summary> ? ? ? ? /// 語速 ? ? ? ? /// </summary> ? ? ? ? private int rate; ? ? ? ? ? public Form2() ? ? ? ? { ? ? ? ? ? ? InitializeComponent(); ? ? ? ? ? ? ReadlocalFile(); ? ? ? ? ? ? comboBox1.SelectedIndex = 0; ? ? ? ? } ? ? ? ? ? private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? rate = Int32.Parse(comboBox1.Text); ? ? ? ? } ? ? ? ? ? //private void 打開文件ToolStripMenuItem_Click(object sender, EventArgs e) ? ? ? ? //{ ? ? ? ? // ? ?this.ReadlocalFile(); ? ? ? ? ? //} ? ? ? ? ? /// <summary> ? ? ? ? /// 讀取本地文本文件的方法 ? ? ? ? /// </summary> ? ? ? ? private void ReadlocalFile() ? ? ? ? { ? ? ? ? ? ? var open = new OpenFileDialog(); ? ? ? ? ? ? ? open.ShowDialog(); ? ? ? ? ? ? ? //得到文件路徑 ? ? ? ? ? ? string path = open.FileName; ? ? ? ? ? ? ? if (path.Trim().Length == 0) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? } ? ? ? ? ? ? ? var os = new StreamReader(path, Encoding.UTF8); ? ? ? ? ? ? string str = os.ReadToEnd(); ? ? ? ? ? ? textBox1.Text = str; ? ? ? ? } ? ? ? ? private void 清空內(nèi)容ToolStripMenuItem_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? textBox1.Text = ""; ? ? ? ? } ? ? ? ? ? private void button1_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? string text = textBox1.Text; ? ? ? ? ? ? ? if (text.Trim().Length == 0) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show("不能閱讀空內(nèi)容!", "錯誤提示"); ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? } ? ? ? ? ? ? ? if (button1.Text == "語音試聽") ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? speech = new SpeechSynthesizer(); ? ? ? ? ? ? ? ? ? new Thread(Speak).Start(); ? ? ? ? ? ? ? ? ? button1.Text = "停止試聽"; ? ? ? ? ? ? ? } ? ? ? ? ? ? else if (button1.Text == "停止試聽") ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? speech.SpeakAsyncCancelAll();//停止閱讀 ? ? ? ? ? ? ? ? ? button1.Text = "語音試聽"; ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? ? private void Speak() ? ? ? ? { ? ? ? ? ? ? ? speech.Rate = rate; ? ? ? ? ? ? //speech.SelectVoice("Microsoft Lili");//設(shè)置播音員(中文) ? ? ? ? ? ? //speech.SelectVoice("Microsoft Anna"); //英文 ? ? ? ? ? ? speech.Volume = value; ? ? ? ? ? ? speech.SpeakAsync(textBox1.Text);//語音閱讀方法 ? ? ? ? ? ? speech.SpeakCompleted += speech_SpeakCompleted;//綁定事件 ? ? ? ? } ? ? ? ? ? /// <summary> ? ? ? ? /// 語音閱讀完成觸發(fā)此事件 ? ? ? ? /// </summary> ? ? ? ? /// <param name="sender"></param> ? ? ? ? /// <param name="e"></param> ? ? ? ? void speech_SpeakCompleted(object sender, SpeakCompletedEventArgs e) ? ? ? ? { ? ? ? ? ? ? button1.Text = "語音試聽"; ? ? ? ? } ? ? ? ? ? /// <summary> ? ? ? ? /// 拖動進(jìn)度條事件 ? ? ? ? /// </summary> ? ? ? ? /// <param name="sender"></param> ? ? ? ? /// <param name="e"></param> ? ? ? ? private void trackBar1_Scroll(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? //因?yàn)閠rackBar1的值為(0-10)之間而音量值為(0-100)所以要乘10; ? ? ? ? ? ? value = trackBar1.Value * 10; ? ? ? ? } ? ? ? ? ? private void button2_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? ? ? ? string text = textBox1.Text; ? ? ? ? ? ? ? if (text.Trim().Length == 0) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? MessageBox.Show("空內(nèi)容無法生成!", "錯誤提示"); ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? } ? ? ? ? ? ? ? this.SaveFile(text); ? ? ? ? ? } ? ? ? ? ? /// <summary> ? ? ? ? /// 生成語音文件的方法 ? ? ? ? /// </summary> ? ? ? ? /// <param name="text"></param> ? ? ? ? private void SaveFile(string text) ? ? ? ? { ? ? ? ? ? ? speech = new SpeechSynthesizer(); ? ? ? ? ? ? var dialog = new SaveFileDialog(); ? ? ? ? ? ? dialog.Filter = "*.wav|*.wav|*.mp3|*.mp3"; ? ? ? ? ? ? dialog.ShowDialog(); ? ? ? ? ? ? ? string path = dialog.FileName; ? ? ? ? ? ? if (path.Trim().Length == 0) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? return; ? ? ? ? ? ? } ? ? ? ? ? ? speech.SetOutputToWaveFile(path); ? ? ? ? ? ? speech.Volume = value; ? ? ? ? ? ? speech.Rate = rate; ? ? ? ? ? ? speech.Speak(text); ? ? ? ? ? ? speech.SetOutputToNull(); ? ? ? ? ? ? MessageBox.Show("生成成功!在" + path + "路徑中!", "提示"); ? ? ? ? ? } ? ? ? ? ? private void label1_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? } ? ? ? ? ? private void label3_Click(object sender, EventArgs e) ? ? ? ? { ? ? ? ? ? ? this.ReadlocalFile(); ? ? ? ? } ? ? ? ? ? } }
意外得知C#豐富的功能,還是自己動手,沒有想象的那么難,希望能幫到有需要的小伙伴們!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)現(xiàn)對圖片文件的壓縮、裁剪操作實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)對圖片文件的壓縮、裁剪操作,較為詳細(xì)的介紹了操作過程中用到的相關(guān)類文件及具體的操作步驟,是非常實(shí)用的技巧,需要的朋友可以參考下2014-09-09淺談C# 非模式窗體show()和模式窗體showdialog()的區(qū)別
下面小編就為大家?guī)硪黄獪\談C# 非模式窗體show()和模式窗體showdialog()的區(qū)別。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-07-07