当前位置:编程学习 > C#/ASP.NET >>

微软 SAPI.SpVoice C# 使用方法 + 实例

 
网上找了好多资料也没找到多少  支持返回当前朗读的位置 用到了C#的回调函数
 
然后为了方便使用 我多此一举的封装了下
 
没什么技术含量 大牛直接飞过
[csharp]  
using System;  
using System.Collections.Generic;  
using System.Text;  
using DotNetSpeech;  
using System.Threading;  
using System.IO;  
  
namespace SpVoiceDemo  
{  
    class SpVoiceUtil  
    {  
        SpVoice voice = new DotNetSpeech.SpVoiceClass();  
  
        public delegate void CallBack(bool b,int InputWordPosition, int InputWordLength);   
  
        /// <summary>  
        /// 朗读文本  
        /// </summary>  
        /// <param name="str">要朗读的文本</param>  
        /// <param name="CallBack">回调地址</param>  
        /// <returns>返回bool</returns>  
        public bool Speak(string str, CallBack CallBack)  
        {  
            int n = voice.Speak(str, SpeechVoiceSpeakFlags.SVSFlagsAsync);  
            Thread thread = new Thread(new ParameterizedThreadStart(Call));  
            thread.IsBackground = true;  
            thread.Start((Object)CallBack);  
            return !(n!=1);  
        }  
  
  
        /// <summary>  
        /// 回调函数线程子程序  
        /// </summary>  
        /// <param name="callBack"></param>  
        private void Call(Object callBack)  
        {  
            int InputWordLength = 0;    //局部_朗读长度  
            int InputWordPosition = 0; //局部_朗读位置  
  
            CallBack CallBack = (CallBack)callBack;  
  
            while ((int)voice.Status.RunningState != 1)  
            {  
                if (InputWordPosition != voice.Status.InputWordPosition || InputWordLength != voice.Status.InputWordLength)  
                {  
                    InputWordPosition = voice.Status.InputWordPosition;  
                    InputWordLength = voice.Status.InputWordLength;  
  
                    //回调                    
                    CallBack(false, InputWordPosition, InputWordLength);  
                }  
            }  
            CallBack(true, InputWordPosition, InputWordLength);  
        }  
  
        /// <summary>  
        /// 获取语音库  
        /// </summary>  
        /// <returns>List<string></returns>  
        public List<string> getDescription()  
        {  
            List<string> list = new List<string>();  
            DotNetSpeech.ISpeechObjectTokens obj = voice.GetVoices();  
            int count = obj.Count;//获取语音库总数  
            for (int i = 0; i < count; i++)  
            {  
               string desc = obj.Item(i).GetDescription(); //遍历语音库  
               list.Add(desc);  
            }  
            return list;  
        }  
  
        /// <summary>  
        /// 设置当前使用语音库  
        /// </summary>  
        /// <returns>bool</returns>  
        public bool setDescription(string name)  
        {  
            List<string> list = new List<string>();  
            DotNetSpeech.ISpeechObjectTokens obj = voice.GetVoices();  
            int count = obj.Count;//获取语音库总数  
            bool result = false;  
            for (int i = 0; i < count; i++)  
            {  
                string desc = obj.Item(i).GetDescription(); //遍历语音库  
                if (desc.Equals(name))  
                {  
                    voice.Voice = obj.Item(i);  
                    result = true;  
                }  
            }  
            return result;  
        }  
  
        /// <summary>  
        /// 设置语速  
        /// </summary>  
        /// <param name="n"></param>  
        public void setRate(int n)  
        {  
            voice
补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,