www.pudn.com > MSTTSSpeech.rar > Speach.cs


using System; 
 
namespace WindowsApplication1 
{ 
	public class Speach  
 
	{  
 
¡¡ private static Speach _Instance = null ;  
 
¡¡ private SpeechLib.SpVoiceClass voice =null;  
 
¡¡ private Speach()  
 
¡¡ {  
 
	  BuildSpeach() ;  
 
¡¡ }  
 
		public static Speach instance()  
 
		{  
 
¡¡ if (_Instance == null)  
 
	  _Instance = new Speach() ;  
 
			return _Instance ;  
 
		}  
 
		private void SetChinaVoice()  
 
		{  
 
¡¡ voice.Voice = voice.GetVoices(string.Empty,string.Empty).Item(0) ;  
 
		}  
 
		private void SetEnglishVoice()  
 
		{  
 
¡¡ voice.Voice = voice.GetVoices(string.Empty,string.Empty).Item(1) ;  
 
		}  
 
		private void SpeakChina(string strSpeak)  
 
		{  
 
¡¡ SetChinaVoice() ;  
 
¡¡ Speak(strSpeak) ;  
 
		}  
 
		private void SpeakEnglishi(string strSpeak)  
 
		{  
 
¡¡ SetEnglishVoice() ;  
 
¡¡ Speak(strSpeak) ;  
 
		}  
 
		public void AnalyseSpeak(string strSpeak)  
 
		{  
 
¡¡ int iCbeg = 0 ;  
 
¡¡ int iEbeg = 0 ;  
 
¡¡ bool IsChina = true ;  
 
¡¡ for(int i=0;i=65)  
 
¡¡ {  
 
	  int iLen = i - iCbeg ;  
 
	  string strValue = strSpeak.Substring(iCbeg,iLen) ;  
 
	  SpeakChina(strValue) ;  
 
	  iEbeg = i ;  
 
	  IsChina = false ;  
 
¡¡ }  
 
	  }  
 
	  else  
 
	  {  
 
¡¡ if (chr>122||chr<65)  
 
¡¡ {  
 
	  int iLen = i - iEbeg ;  
 
	  string strValue = strSpeak.Substring(iEbeg,iLen) ;  
 
	  this.SpeakEnglishi(strValue) ;  
 
	  iCbeg = i ;  
 
	  IsChina = true ;  
 
¡¡ }  
 
	  }  
 
¡¡ }//end for  
 
¡¡ if (IsChina)  
 
¡¡ {  
 
	  int iLen = strSpeak.Length - iCbeg ;  
 
	  string strValue = strSpeak.Substring(iCbeg,iLen) ;  
 
	  SpeakChina(strValue) ;  
 
¡¡ }  
 
¡¡ else  
 
¡¡ {  
 
	  int iLen = strSpeak.Length - iEbeg ;  
 
	  string strValue = strSpeak.Substring(iEbeg,iLen) ;  
 
	  SpeakEnglishi(strValue) ;  
 
¡¡ }  
 
		}  
 
		private void BuildSpeach()  
 
		{  
 
¡¡ if (voice == null)  
 
	  voice = new SpVoiceClass() ;  
 
		}  
 
		public int Volume  
 
		{  
 
¡¡ get  
 
¡¡ {  
 
	  return voice.Volume ;  
 
¡¡ }  
 
¡¡ set  
 
¡¡ {  
 
	  voice.SetVolume((ushort)(value)) ;  
 
¡¡ }  
 
		}  
 
		public int Rate  
 
		{  
 
¡¡ get  
 
¡¡ {  
 
	  return voice.Rate ;  
 
¡¡ }  
 
¡¡ set  
 
¡¡ {  
 
	  voice.SetRate(value) ;  
 
¡¡ }  
 
		}  
 
		private void Speak(string strSpeack)  
 
		{  
 
¡¡ try  
 
¡¡ {  
 
	  voice.Speak(strSpeack,SpeechVoiceSpeakFlags.SVSFlagsAsync) ;  
 
¡¡ }  
 
¡¡ catch(Exception err)  
 
¡¡ {  
 
	  throw(new Exception("·¢ÉúÒ»¸ö´íÎó£º"+err.Message)) ;  
 
¡¡ }  
 
		}  
 
		public void Stop()  
 
		{  
 
¡¡ voice.Speak(string.Empty,SpeechLib.SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak) ;  
 
		}  
 
		public void Pause()  
 
		{  
 
¡¡ voice.Pause() ;  
 
		}  
 
		public void Continue()  
 
		{  
 
¡¡ voice.Resume() ;  
 
		}  
 
	}//end class  
 
}