zoukankan      html  css  js  c++  java
  • C#实现语音朗读功能

    第一步:新建项目  TTS(从文本到语音(TextToSpeech))

    第二步:添加引用 System.Speech

    第三步:主界面以及后台代码

    using System;
    using System.Globalization;
    using System.Linq;
    using System.Speech.Synthesis;
    using System.Windows.Forms;

    namespace TTS
    {
    public partial class Form1 : Form
    {
    private SpeechSynthesizer speech = new SpeechSynthesizer();

    public Form1()
    {
    InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {

    #region 只能读数字和英文
    //string text = textBox1.Text;

    //if (text.Trim().Length != 0)
    //{
    // speech.Rate = 5;//语速
    // speech.SelectVoice("Microsoft Lili");//设置播音员(中文)
    // //speech.SelectVoice("Microsoft Anna"); //英文
    // speech.Volume = 100; //音量
    // speech.SpeakAsync(textBox1.Text);//语音阅读方法
    //}
    #endregion

    #region 可以读取中文
    string phrase = "123我是好人";
    SpeechSynthesizer speech = new SpeechSynthesizer();
    CultureInfo keyboardCulture = System.Windows.Forms.InputLanguage.CurrentInputLanguage.Culture;
    InstalledVoice neededVoice = speech.GetInstalledVoices(keyboardCulture).FirstOrDefault();
    if (neededVoice == null)
    {
    phrase = "Unsupported Language";
    }
    else if (!neededVoice.Enabled)
    {
    phrase = "Voice Disabled";
    }
    else
    {
    speech.SelectVoice(neededVoice.VoiceInfo.Name);
    }

    speech.Speak(phrase);
    #endregion
    }
    }
    }

    源代码下载:https://download.csdn.net/download/longtenggenssupreme/10452762

    注:本代码是在是在windows server 2012 R2 上的vs2017 上的4.5版本的环境。

     建议Win10 环境

    如果不是上述环境,可能会出现以下:

     1、报错

     发生了 System.IO.FileNotFoundException
      HResult=0x8007007E
      Message=检索 COM 类工厂中 CLSID 为 {D9F6EE60-58C9-458B-88E1-2F908FD7F87C} 的组件失败,原因是出现以下错误: 8007007e 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)。
      Source=System.Speech
      StackTrace:
       在 System.Speech.Internal.ObjectTokens.RegistryDataKey..ctor(String fullPath, IntPtr regHandle)
       在 System.Speech.Internal.ObjectTokens.RegistryDataKey.Open(String registryPath, Boolean fCreateIfNotExist)
       在 System.Speech.Internal.ObjectTokens.SAPICategories.DefaultDeviceOut()
       在 System.Speech.Internal.Synthesis.VoiceSynthesis..ctor(WeakReference speechSynthesizer)
       在 System.Speech.Synthesis.SpeechSynthesizer.get_VoiceSynthesizer()
       在 System.Speech.Synthesis.SpeechSynthesizer.set_Rate(Int32 value)
       在 TTS.Form1.button1_Click(Object sender, EventArgs e) 在 C:UsersAdministratorDesktopTTSForm1.cs 中: 第 26 行
       在 System.Windows.Forms.Control.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       在 System.Windows.Forms.Control.WndProc(Message& m)
       在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
       在 System.Windows.Forms.Button.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.Run(Form mainForm)
       在 TTS.Program.Main() 在 C:UsersAdministratorDesktopTTSProgram.cs 中: 第 19 行

    2、或者是下面错误

  • 相关阅读:
    SSH框架总结
    SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
    iBATIS SQL Maps
    深入理解Mybatis中sqlSessionFactory机制原理
    mybatis源码分析(1)——SqlSessionFactory实例的产生过程
    MyBatis常用对象SqlSessionFactory和SqlSession介绍和运用
    Generator生成器函数
    MyBatis学习4---使用MyBatis_Generator生成Dto、Dao、Mapping
    采用图形用户界面的操作系统/应用程序
    图形界面的特点是人们不需要记忆和键入繁琐的命令
  • 原文地址:https://www.cnblogs.com/1175429393wljblog/p/9122135.html
Copyright © 2011-2022 走看看