首先,添加DotNetSpeech.dll引用,可以在這里直接下載,也可以參照這篇文章說的,在安裝Speech SDK以后,在Common Files\Microsoft Shared\Speech\目錄下面找到SAPI.dll,用Tlbimp.exe工具將該dll轉(zhuǎn)換成.net平臺下的Assembly---DotNetSpeech.dll。
然后,using DotNetSpeech;
朗讀功能:
using DotNetSpeech;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Speech.Synthesis;
using System.Speech;
namespace MyProject
{
public class ValuesController : ApiController
{
public SpeechSynthesizer synth; //語音合成對象
GET api/<controller>
//public IEnumerable<string> Get()
//{
// return new string[] { "value1", "value2" };
//}
GET api/<controller>/5
//public string Get(string cont)
//{
// return cont;
//}
POST api/<controller>
//public void Post([FromBody] string value)
//{
//}
PUT api/<controller>/5
//public void Put(int id, [FromBody] string value)
//{
//}
DELETE api/<controller>/5
//public void Delete(int id)
//{
//}
//[HttpPost]
[HttpGet]
public string ToCall(string cont)
{
//調(diào)用示例:http://192.168.6.195:8081/api/values/ToCall?cont=請,劉笑笑,李秀秀,導(dǎo)醫(yī)臺領(lǐng)結(jié)果吧
//https://localhost:44399/api/values/ToCall?cont=請,劉笑笑,李秀秀,導(dǎo)醫(yī)臺領(lǐng)結(jié)果吧
//SpeechVoiceSpeakFlags flags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
//SpVoice sp = new SpVoice();
sp.Voice = sp.GetVoices(" name=Microsoft Simplified Chinese ", "").Item(0);
//sp.Voice = sp.GetVoices(string.Empty, string.Empty).Item(0); //0選擇默認(rèn)的語音,
//sp.Rate = 0;//語速
//sp.Volume = 100;//音量
//sp.Speak(cont, flags);
synth = new SpeechSynthesizer();
//使用 synth 設(shè)置朗讀音量 [范圍 0 ~ 100]
??????????? synth.Volume = 100;
??????????? //使用 synth 設(shè)置朗讀頻率 [范圍 -10 ~ 10]
??????????? synth.Rate = 0;
synth.SelectVoice(synth.GetInstalledVoices()[0].VoiceInfo.Name);
//synth.SelectVoice("Microsoft Lili");
??????????? //Voice.Speak(ggg, SpFlags);
??????????? synth.SpeakAsync(cont);
return "12345";
}
[HttpGet]
public string GetAll()
{
return "Success";
}
}
}
注:SpeechVoiceSpeakFlags是語音朗讀的風(fēng)格;?Voice中是語音類型(語言、男(女)聲),有?Microsoft Simplified Chinese,Microsoft Mary(Sam,Mike)等,
也可以這樣:voice.Voice?=?voice.GetVoices(string.Empty,?string.Empty).Item(0);?//0選擇默認(rèn)的語音,
1選擇第二個語音;Rate指的是語速。
當(dāng)然,你也可以在此不寫,打開控制面板中的語音設(shè)置類型和語速
生成語音文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Speech.Synthesis;
using System.Collections.ObjectModel;
using System.Web.Configuration;
using DotNetSpeech;
using Microsoft.Win32;
using System.Windows.Forms;
using System.Threading;
SpeechVoiceSpeakFlags flags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
SpVoice sp = new SpVoice();
//sp.Voice = sp.GetVoices(" name=Microsoft Simplified Chinese ", "").Item(0);
sp.Voice = sp.GetVoices(string.Empty, string.Empty).Item(0); //0選擇默認(rèn)的語音,
sp.Rate = 0;//語速
sp.Speak(strCont, flags);
System.Windows.Forms.SaveFileDialog dialog = new System.Windows.Forms.SaveFileDialog();
dialog.Filter = "?All files (*.*)|*.*|wav files (*.wav)|*.wav?";
dialog.Title = "?Save to a wave file?";
dialog.FilterIndex = 2;
dialog.RestoreDirectory = true;
if (dialog.ShowDialog() == DialogResult.OK)
{
SpeechStreamFileMode spFileMode = SpeechStreamFileMode.SSFMCreateForWrite;
SpFileStream spFileStream = new SpFileStream();
spFileStream.Open(dialog.FileName, spFileMode, false);
sp.AudioOutputStream = spFileStream;
sp.Speak("文字轉(zhuǎn)語音的內(nèi)容", flags);
sp.WaitUntilDone(Timeout.Infinite);
spFileStream.Close();
}
(在WinForm和Web中都適用)
參考:http://www.microsoft.com/china/community/program/originalarticles/TechDoc/Cnspeech.mspx
????????使用語音即時校對輸入內(nèi)容 - 斯克迪亞 - 博客園文章來源:http://www.zghlxwxcb.cn/news/detail-466871.html
轉(zhuǎn)載于:https://www.cnblogs.com/pfs1314/archive/2011/01/11/1932870.html文章來源地址http://www.zghlxwxcb.cn/news/detail-466871.html
到了這里,關(guān)于.net實現(xiàn)簡單語音朗讀(TTS)功能的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!