.net实现语音朗读(TTS)功能
首先,添加DotNetSpeech.dll引用,可以在这里直接下载,也可以参照这篇文章说的,在安装Speech SDK以后,在Common Files\Microsoft Shared\Speech\目录下面找到SAPI.dll,用Tlbimp.exe工具将该dll转换成.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]
-
[]
-
public string ToCall(string cont)
-
{
-
//调用示例:http://192.168.6.195:8081/api/values/ToCall?cont=请,刘笑笑,李秀秀,导医台领结果吧
-
//https://localhost:44399/api/values/ToCall?cont=请,刘笑笑,李秀秀,导医台领结果吧
-
-
-
//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选择默认的语音,
-
//sp.Rate = 0;//语速
-
//sp.Volume = 100;//音量
-
//sp.Speak(cont, flags);
-
-
synth = new SpeechSynthesizer();
-
//使用 synth 设置朗读音量 [范围 0 ~ 100]
-
synth.Volume = 100;
-
//使用 synth 设置朗读频率 [范围 -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";
-
-
}
-
-
-
[]
-
public string GetAll()
-
{
-
return "Success";
-
}
-
-
-
-
}
-
}
注:SpeechVoiceSpeakFlags是语音朗读的风格; Voice中是语音类型(语言、男(女)声),有 Microsoft Simplified Chinese,Microsoft Mary(Sam,Mike)等,
也可以这样:voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(0); //0选择默认的语音,
1选择第二个语音;Rate指的是语速。
当然,你也可以在此不写,打开控制面板中的语音设置类型和语速
生成语音文件:
-
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选择默认的语音,
-
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("文字转语音的内容", flags);
-
sp.WaitUntilDone(Timeout.Infinite);
-
spFileStream.Close();
-
}
(在WinForm和Web中都适用)
参考:http://www.microsoft.com/china/community/program/originalarticles/TechDoc/Cnspeech.mspx
转载于:https://www.cnblogs.com/pfs1314/archive/2011/01/11/1932870.html
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhfiikhk
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01