C#实现语音朗读功能

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

C#实现语音朗读功能_第1张图片

 

第二步:添加引用 System.Speech

C#实现语音朗读功能_第2张图片

C#实现语音朗读功能_第3张图片

 

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

C#实现语音朗读功能_第4张图片

 

 

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、报错

C#实现语音朗读功能_第5张图片
 发生了 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:\Users\Administrator\Desktop\TTS\Form1.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:\Users\Administrator\Desktop\TTS\Program.cs 中: 第 19 行

2、或者是下面错误

C#实现语音朗读功能_第6张图片

你可能感兴趣的:(C#实现语音朗读功能)