• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

.net实现语音朗读(TTS)功能

武飞扬头像
wybshyy
帮助1

首先,添加DotNetSpeech.dll引用,可以在这里直接下载,也可以参照这篇文章说的,在安装Speech SDK以后,在Common Files\Microsoft Shared\Speech\目录下面找到SAPI.dll,用Tlbimp.exe工具将该dll转换成.net平台下的Assembly---DotNetSpeech.dll。

然后,using DotNetSpeech;

朗读功能:

  1.  
    using DotNetSpeech;
  2.  
    using System;
  3.  
    using System.Collections.Generic;
  4.  
    using System.Linq;
  5.  
    using System.Net;
  6.  
    using System.Net.Http;
  7.  
    using System.Web.Http;
  8.  
    using System.Speech.Synthesis;
  9.  
    using System.Speech;
  10.  
     
  11.  
    namespace MyProject
  12.  
    {
  13.  
    public class ValuesController : ApiController
  14.  
    {
  15.  
    public SpeechSynthesizer synth; //语音合成对象
  16.  
     
  17.  
    GET api/<controller>
  18.  
    //public IEnumerable<string> Get()
  19.  
    //{
  20.  
    // return new string[] { "value1", "value2" };
  21.  
    //}
  22.  
     
  23.  
    GET api/<controller>/5
  24.  
    //public string Get(string cont)
  25.  
    //{
  26.  
    // return cont;
  27.  
    //}
  28.  
     
  29.  
    POST api/<controller>
  30.  
    //public void Post([FromBody] string value)
  31.  
    //{
  32.  
    //}
  33.  
     
  34.  
    PUT api/<controller>/5
  35.  
    //public void Put(int id, [FromBody] string value)
  36.  
    //{
  37.  
    //}
  38.  
     
  39.  
    DELETE api/<controller>/5
  40.  
    //public void Delete(int id)
  41.  
    //{
  42.  
    //}
  43.  
     
  44.  
    //[HttpPost]
  45.  
    [HttpGet]
  46.  
    public string ToCall(string cont)
  47.  
    {
  48.  
    //调用示例:http://192.168.6.195:8081/api/values/ToCall?cont=请,刘笑笑,李秀秀,导医台领结果吧
  49.  
    //https://localhost:44399/api/values/ToCall?cont=请,刘笑笑,李秀秀,导医台领结果吧
  50.  
     
  51.  
     
  52.  
    //SpeechVoiceSpeakFlags flags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
  53.  
    //SpVoice sp = new SpVoice();
  54.  
    sp.Voice = sp.GetVoices(" name=Microsoft Simplified Chinese ", "").Item(0);
  55.  
    //sp.Voice = sp.GetVoices(string.Empty, string.Empty).Item(0); //0选择默认的语音,
  56.  
    //sp.Rate = 0;//语速
  57.  
    //sp.Volume = 100;//音量
  58.  
    //sp.Speak(cont, flags);
  59.  
     
  60.  
    synth = new SpeechSynthesizer();
  61.  
    //使用 synth 设置朗读音量 [范围 0 ~ 100]
  62.  
                synth.Volume = 100;
  63.  
                //使用 synth 设置朗读频率 [范围 -10 ~ 10]
  64.  
                synth.Rate = 0;
  65.  
     
  66.  
     
  67.  
    synth.SelectVoice(synth.GetInstalledVoices()[0].VoiceInfo.Name);
  68.  
    //synth.SelectVoice("Microsoft Lili");
  69.  
                //Voice.Speak(ggg, SpFlags);
  70.  
                synth.SpeakAsync(cont);
  71.  
     
  72.  
     
  73.  
     
  74.  
     
  75.  
    return "12345";
  76.  
     
  77.  
    }
  78.  
     
  79.  
     
  80.  
    [HttpGet]
  81.  
    public string GetAll()
  82.  
    {
  83.  
    return "Success";
  84.  
    }
  85.  
     
  86.  
     
  87.  
     
  88.  
    }
  89.  
    }
学新通

注:SpeechVoiceSpeakFlags是语音朗读的风格; Voice中是语音类型(语言、男(女)声),有 Microsoft Simplified Chinese,Microsoft Mary(Sam,Mike)等,

也可以这样:voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(0); //0选择默认的语音,
1选择第二个语音;Rate指的是语速。

当然,你也可以在此不写,打开控制面板中的语音设置类型和语速

生成语音文件:

  1.  
    using System;
  2.  
    using System.Collections.Generic;
  3.  
    using System.Linq;
  4.  
    using System.Web;
  5.  
    using System.Speech.Synthesis;
  6.  
    using System.Collections.ObjectModel;
  7.  
    using System.Web.Configuration;
  8.  
    using DotNetSpeech;
  9.  
    using Microsoft.Win32;
  10.  
    using System.Windows.Forms;
  11.  
    using System.Threading;
  12.  
     
  13.  
     
  14.  
    SpeechVoiceSpeakFlags flags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
  15.  
    SpVoice sp = new SpVoice();
  16.  
    //sp.Voice = sp.GetVoices(" name=Microsoft Simplified Chinese ", "").Item(0);
  17.  
    sp.Voice = sp.GetVoices(string.Empty, string.Empty).Item(0); //0选择默认的语音,
  18.  
    sp.Rate = 0;//语速
  19.  
    sp.Speak(strCont, flags);
  20.  
     
  21.  
    System.Windows.Forms.SaveFileDialog dialog = new System.Windows.Forms.SaveFileDialog();
  22.  
    dialog.Filter = " All files (*.*)|*.*|wav files (*.wav)|*.wav ";
  23.  
    dialog.Title = " Save to a wave file ";
  24.  
    dialog.FilterIndex = 2;
  25.  
    dialog.RestoreDirectory = true;
  26.  
    if (dialog.ShowDialog() == DialogResult.OK)
  27.  
    {
  28.  
    SpeechStreamFileMode spFileMode = SpeechStreamFileMode.SSFMCreateForWrite;
  29.  
    SpFileStream spFileStream = new SpFileStream();
  30.  
    spFileStream.Open(dialog.FileName, spFileMode, false);
  31.  
    sp.AudioOutputStream = spFileStream;
  32.  
    sp.Speak("文字转语音的内容", flags);
  33.  
    sp.WaitUntilDone(Timeout.Infinite);
  34.  
    spFileStream.Close();
  35.  
    }
学新通

(在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
系列文章
更多 icon
同类精品
更多 icon
继续加载