每次都要引用满麻烦的 修改下不用引用了
使用方法
SPVoic S = new SPVoic();
S.Rate = 0;
S.SpeakSave("You have selected Microsoft Sam as the computer's default voice.", @"c:/1.wav");
S.Speak("You have selected Microsoft Sam as the computer's default voice.");
全部的类
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace Zgke.Media
{
/// <summary>
/// 调用WINDOWS的 语音
/// zgke@sina.com
/// qq:116149
/// </summary>
public class SPVoic
{
/*
* SPVoic S = new SPVoic();
* S.Rate = 0;
* S.SpeakSave("You have selected Microsoft Sam as the computer's default voice.", @"c:/1.wav");
* S.Speak("You have selected Microsoft Sam as the computer's default voice.");
*
*/
private object m_SpVoice = null;
private Type m_SpVoiceType = null;
private IList<object> m_Voices = new List<object>();
private int m_VoiceIndex = 0;
private int m_Rate = 0;
private object m_AudioOutputStream = null;
/// <summary>
/// 可用语音数量
/// </summary>
public int VoicesCount { get { return m_Voices.Count; } }
/// <summary>
/// 获取中文字符集
/// </summary>
/// <param name="p_Index"></param>
/// <returns></returns>
public string this[int p_Index]
{
get
{
if (p_Index < 0 || p_Index >= m_Voices.Count) throw new Exception("下标越界");
return (string)m_Voices[p_Index].GetType().InvokeMember("GetDescription", BindingFlags.Instance | BindingFlags.InvokeMethod, null, m_Voices[p_Index], new object[] { });
}
}
/// <summary>
/// 获取和设置 使用语音
/// </summary>
public int VoiceIndex
{
get { return m_VoiceIndex; }
set { if (value < 0 || value >= m_Voices.Count) throw new Exception("下标越界"); m_VoiceIndex = value; }
}
/// <summary>
/// 语音速度
/// </summary>
public int Rate
{
get { return m_Rate; }
set { m_Rate = value; }
}
public SPVoic()
{
m_SpVoiceType = Type.GetTypeFromProgID("SAPI.SpVoice");
if (m_SpVoiceType == null) throw new Exception("无法获取SPVOIC");
m_SpVoice = Activator.CreateInstance(m_SpVoiceType);
GetList();
//获取默认播放类型
m_AudioOutputStream = m_SpVoiceType.InvokeMember("AudioOutputStream", BindingFlags.Instance | BindingFlags.GetProperty, null, m_SpVoice, new object[] { });
}
/// <summary>
/// 获取可用集合
/// </summary>
private void GetList()
{
object _Voices = m_SpVoiceType.InvokeMember("GetVoices", BindingFlags.Instance | BindingFlags.InvokeMethod, null, m_SpVoice, new object[] { });
//获取集合数量
int _Count = (int)_Voices.GetType().InvokeMember("Count", BindingFlags.Instance | BindingFlags.GetProperty, null, _Voices, new object[] { });
m_Voices.Clear();
for (int i = 0; i != _Count; i++)
{
//取出一种声音
object _Voice = _Voices.GetType().InvokeMember("Item", BindingFlags.Instance | BindingFlags.InvokeMethod, null, _Voices, new object[] { i });
m_Voices.Add(_Voice);
}
}
/// <summary>
/// 播放文字
/// </summary>
/// <param name="p_Text">播放文字</param>
/// <param name="p_VoiceIndex">语音集索引</param>
/// <param name="p_Rate">语音速度</param>
public void Speak(string p_Text)
{
//设置默认播放类型
m_SpVoiceType.InvokeMember("AudioOutputStream", BindingFlags.Instance | BindingFlags.SetProperty, null, m_SpVoice, new object[] { m_AudioOutputStream });
//设置语音
m_SpVoiceType.InvokeMember("Voice", BindingFlags.Instance | BindingFlags.SetProperty, null, m_SpVoice, new object[] { m_Voices[m_VoiceIndex] });
//设置速度
m_SpVoiceType.InvokeMember("Rate", BindingFlags.Instance | BindingFlags.SetProperty, null, m_SpVoice, new object[] { m_Rate });
//开始播放
m_SpVoiceType.InvokeMember("Speak", BindingFlags.Instance | BindingFlags.InvokeMethod, null, m_SpVoice, new object[] { p_Text, 1 });
}
public void SpeakSave(string p_Text, string p_File)
{
object _SpFileStream = Activator.CreateInstance(Type.GetTypeFromProgID("SAPI.SpFileStream"));
try
{
//使用SP FileStream打开文件
_SpFileStream.GetType().InvokeMember("Open", BindingFlags.Instance | BindingFlags.InvokeMethod, null, _SpFileStream, new object[] { p_File, 3, false });
//设置语音
m_SpVoiceType.InvokeMember("Voice", BindingFlags.Instance | BindingFlags.SetProperty, null, m_SpVoice, new object[] { m_Voices[m_VoiceIndex] });
//设置速度
m_SpVoiceType.InvokeMember("Rate", BindingFlags.Instance | BindingFlags.SetProperty, null, m_SpVoice, new object[] { m_Rate });
//设置播放到那里
m_SpVoiceType.InvokeMember("AudioOutputStream", BindingFlags.Instance | BindingFlags.SetProperty, null, m_SpVoice, new object[] { _SpFileStream });
//开始播放
m_SpVoiceType.InvokeMember("Speak", BindingFlags.Instance | BindingFlags.InvokeMethod, null, m_SpVoice, new object[] { p_Text, 1 });
//等待播放结束
m_SpVoiceType.InvokeMember("WaitUntilDone", BindingFlags.Instance | BindingFlags.InvokeMethod, null, m_SpVoice, new object[] { -1 });
//关闭文件
_SpFileStream.GetType().InvokeMember("Close", BindingFlags.Instance | BindingFlags.InvokeMethod, null, _SpFileStream, new object[] { });
}
finally
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(_SpFileStream);
}
}
}
}