c#實現語音閱讀以及文本轉語音文件是基於c#的一個類庫(SpeechSynthesizer )實現的,使用該類必需要添加引用using System.Speech.Synthesis;直接是沒法添加引用的,先對項目進行添加應用c#
示例圖this
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.Windows.Forms;
spa
namespace 文本轉語音Demo
{
public partial class Form1 : Form
{
private SpeechSynthesizer speech;
/// <summary>
/// 音量
/// </summary>
private int value=100;
/// <summary>
/// 語速
/// </summary>
private int rate;
public Form1()
{
InitializeComponent();
comboBox1.SelectedIndex = 10;
} 3d
private void button1_Click(object sender, EventArgs e)
{
string text=textBox1.Text;orm
if (text.Trim().Length == 0) {
MessageBox.Show("不能閱讀空內容!","錯誤提示");
return;
}
if (button1.Text == "語音試聽") { blog
speech = new SpeechSynthesizer();事件
new Thread(Speak).Start();ip
button1.Text = "中止試聽";string
}else if(button1.Text=="中止試聽"){it
speech.SpeakAsyncCancelAll();//中止閱讀
button1.Text = "語音試聽";
}
}
private void Speak() {
speech.Rate =rate;
speech.SelectVoice("Microsoft Lili");//設置播音員(中文)
//speech.SelectVoice("Microsoft Anna"); //英文
speech.Volume = value;
speech.SpeakAsync(textBox1.Text);//語音閱讀方法
speech.SpeakCompleted += speech_SpeakCompleted;//綁定事件
}
/// <summary>
/// 語音閱讀完成觸發此事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void speech_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
{
button1.Text = "語音試聽";
}
/// <summary>
/// 拖動進度條事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void trackBar1_Scroll(object sender, EventArgs e)
{
//由於trackBar1的值爲(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("空內容沒法生成!", "錯誤提示");
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 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 清空內容ToolStripMenuItem_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
}
}
程序運行效果截圖