1. 利用DONET框架自带的 SpeechSynthesizer ,缺点是没有感情色彩,抑扬顿挫等。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Speech.Synthesis;
using System.Threading;
using Microsoft.Win32;
namespace MSSpeech
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
speech = new SpeechSynthesizer();
speech.Rate = rate;
// speech.SelectVoice("Microsoft Lili");//设置播音员(中文)
speech.SelectVoice("Microsoft Anna"); //英文
speech.Volume = value;
speech.SpeakCompleted += speech_SpeakCompleted;//绑定事件
Closing += new System.ComponentModel.CancelEventHandler(MainWindow_Closing);
}
void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
speech.SpeakAsyncCancelAll();//停止阅读
speech.SpeakCompleted -= speech_SpeakCompleted;
}
private SpeechSynthesizer speech;
/// <summary>
/// 音量
/// </summary>
private int value = 100;
/// <summary>
/// 语速
/// </summary>
private int rate;
private string words = "";
private void btnSpeech_Click(object sender, RoutedEventArgs e)
{
test();
}
void test()
{
string text = textBox1.Text;
if (text.Trim().Length == 0)
{
MessageBox.Show("不能阅读空内容!", "错误提示");
return;
}
words = textBox1.Text;
new Thread(Speak).Start();
}
private void Speak()
{
speech.SpeakAsync(words);//语音阅读方法
}
/// <summary>
/// 语音阅读完成触发此事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void speech_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
{
btnSpeech.Content = "语音试听";
}
// <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 + "路径中!", "提示");
}
}
}
2. 百度语音合成,在线模式,缺点需要联网发送请求,如果文本太多就会有延迟问题需要解决。
请求API例子:
Request URL:http://ai.baidu.com/aidemo
Request Method:POST
Status Code:200 OK
Remote Address:220.181.164.109:80
Referrer Policy:no-referrer-when-downgrade
Response Headers
view source
Access-Control-Allow-Origin:*
Connection:keep-alive
Content-Type:text/json; charset=UTF-8
Date:Wed, 26 Sep 2018 07:41:51 GMT
Server:Apache
Tracecode:25108570390947468554092615
Tracecode:25108562210900343306092615
Transfer-Encoding:chunked
Request Headers
view source
Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:751
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:BAIDUID=2BF1001DCF35B79BEFB7C9C63C0C90B7:FG=1; BIDUPSID=2BF1001DCF35B79BEFB7C9C63C0C90B7; PSTM=1517209070; H_PS_PSSID=26523_1431_21080_26350_20928; Hm_lvt_8b973192450250dd85b9011320b455ba=1537947694; Hm_lpvt_8b973192450250dd85b9011320b455ba=1537947694; seccode=45956e63379931ef6aa0f3c472ad195c
Host:ai.baidu.com
Origin:http://ai.baidu.com
Referer:http://ai.baidu.com/tech/speech/tts
User-Agent:Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Mobile Safari/537.36
X-Requested-With:XMLHttpRequest
Form Data
view source
view URL encoded
type:tns
spd:5
vol:5
per:4
tex:百度语音,面向广大开发者永久免费开放语音合成技术。所采用的离在线融合技术,根据当前网络状况,自动判断使用本地引擎或云端引擎,进行语音合成,再也不用担心流量消耗了
3. 用 DotNetSpeech.dll 第三方组件,缺点 朗读比较生硬,语音库和本地系统相关。
代码:
SpVoice sp = new SpVoice();
sp.Rate = GetSpeedSelected();
SpeechVoiceSpeakFlags sFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
sp.Speak(text, sFlags);