使用微软的SAPI实现文本转成声音,功能基本可以实现,效果达不到非常满意。其中有几个问题还没有完全解决,望哪位高手帮忙指点,谢谢啦。
1、标点符号和英文单词之间没有空格分隔,标点符号问号?读成问题question。
2、怎么设置停顿 ?解决方法:使用标点符号?停顿,例如:A ? How are you?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SpeechLib;
using System.Collections;
namespace SpeechDemo
{
public class SpeechHelper
{
#region Field
private SpeechLib.SpVoiceClass voice = null;
private static SpeechHelper instance;
#endregion
#region Contructor
private SpeechHelper()
{
if (voice == null)
{
voice = new SpeechLib.SpVoiceClass();
voice.EndStream += new _ISpeechVoiceEvents_EndStreamEventHandler(voice_EndStream);
}
}
#endregion
#region Event
public delegate void EndStreamEventHandler();
public event EndStreamEventHandler EndStream;
#endregion
#region Method
void voice_EndStream(int StreamNumber, object StreamPosition)
{
if (EndStream != null)
{
EndStream();
}
}
public static SpeechHelper GetInstance()
{
if (instance == null)
{
instance = new SpeechHelper();
}
return instance;
}
public void SetRate(int rate)
{
voice.Rate = rate;
}
public void SetVolume(int volume)
{
voice.Volume = volume;
}
public void SetRole(int role)
{
voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(role);
}
public void Speak(string text)
{
if (string.IsNullOrEmpty(text))
{
return;
}
voice.Speak(text, SpeechVoiceSpeakFlags.SVSFlagsAsync);
}
public void Resume()
{
voice.Resume();
}
public void Pause()
{
voice.Pause();
}
public void Stop()
{
voice.Speak(string.Empty, SpeechLib.SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
}
public ArrayList GetAllRole()
{
ArrayList roleArr = new ArrayList();
for (int i = 0; i < voice.GetVoices(string.Empty, string.Empty).Count; i++)
{
roleArr.Add(voice.GetVoices(string.Empty, string.Empty).Item(i).GetDescription(0));
}
return roleArr;
}
#endregion
}
}