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_Clickobject 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.SpeakAsynctextBox1.Text);//语音阅读方法
//}
#endregion

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

speech.Speakphrase);
#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..ctorString fullPath, IntPtr regHandle)
   在 System.Speech.Internal.ObjectTokens.RegistryDataKey.OpenString registryPath, Boolean fCreateIfNotExist)
   在 System.Speech.Internal.ObjectTokens.SAPICategories.DefaultDeviceOut)
   在 System.Speech.Internal.Synthesis.VoiceSynthesis..ctorWeakReference speechSynthesizer)
   在 System.Speech.Synthesis.SpeechSynthesizer.get_VoiceSynthesizer)
   在 System.Speech.Synthesis.SpeechSynthesizer.set_RateInt32 value)
   在 TTS.Form1.button1_ClickObject sender, EventArgs e) 在 C:UsersAdministratorDesktopTTSForm1.cs 中: 第 26 行
   在 System.Windows.Forms.Control.OnClickEventArgs e)
   在 System.Windows.Forms.Button.OnClickEventArgs e)
   在 System.Windows.Forms.Button.OnMouseUpMouseEventArgs mevent)
   在 System.Windows.Forms.Control.WmMouseUpMessage& m, MouseButtons button, Int32 clicks)
   在 System.Windows.Forms.Control.WndProcMessage& m)
   在 System.Windows.Forms.ButtonBase.WndProcMessage& m)
   在 System.Windows.Forms.Button.WndProcMessage& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.OnMessageMessage& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.WndProcMessage& m)
   在 System.Windows.Forms.NativeWindow.DebuggableCallbackIntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageWMSG& msg)
   在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoopIntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInnerInt32 reason, ApplicationContext context)
   在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInt32 reason, ApplicationContext context)
   在 System.Windows.Forms.Application.RunForm mainForm)
   在 TTS.Program.Main) 在 C:UsersAdministratorDesktopTTSProgram.cs 中: 第 19 行

2、或者是下面错误

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注