1.[DllImport("winmm.dll", EntryPoint = "waveOutGetNumDevs")]
public static extern int waveOutGetNumDevs();
2.using SpeechLib;
SpeechVoiceSpeakFlags svsf;
SpVoiceClass svc = null;
3.右击Interop.SpeechLib ->嵌入式互操作类型,选择false
4.svc.Speak(content.Trim(), svsf);
完整代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SpeechLib;
using System.Runtime.InteropServices;
using System.Threading;
public class Speak
{
SpeechVoiceSpeakFlags svsf;
SpVoiceClass svc = null;
//增值为+10
private Int16 _setVolume = 100;
public Speak()
{
try
{
svsf = new SpeechVoiceSpeakFlags();
svc = new SpVoiceClass();
svc.SetVolume(Convert.ToUInt16(_setVolume));
}
catch (Exception ex)
{
throw ex;
}
}
[DllImport("winmm.dll", EntryPoint = "waveOutGetNumDevs")]
public static extern int waveOutGetNumDevs();
public void Speak(string content)
{
try
{
if (string.IsNullOrEmpty(content.Trim()))
{
return;
}
Thread.Sleep(100);
svc.Speak(content.Trim(), svsf);
}
catch (Exception ex)
{
throw ex;
}
}
}