C#使用反射调用SpVoice实现播放文本

每次都要引用满麻烦的 修改下不用引用了

 

使用方法

          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);
            }
        }

    }
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
SpVoice是一个在Windows操作系统上用于文字到语音转换的COM组件。使用SpVoice可以将电脑上的文字转换成听得到的语音。以下是SpVoice使用方法。 1. 引用SpVoice组件:在开发环境中创建一个新项目,并确保引用了SpVoice组件。在Visual Studio中,可以在项目属性的“引用”中添加对SpeechLib的引用。 2. 创建SpVoice对象:使用SpVoice对象可以实现文字到语音的转换。在代码中创建一个SpVoice对象,如:Dim objVoice As New SpVoice()。 3. 设置语音属性:可以通过SpVoice对象的属性来设置语音的属性,如音量、语速、声音等。例如,可以使用objVoice.Volume属性设置音量大小,范围从0(静音)到100。 4. 设置语言:可以使用objVoice.Voice属性来设置语音的语言。可以使用GetVoices方法获取系统上可用的语言,然后使用Voice对象的ID属性来设置语言。 5. 加载文本并转换成语音:使用Speak方法将文本转换为语音。可以将要转换的文本作为Speak方法的参数传入,如:objVoice.Speak("你好,世界")。 6. 控制语音播放:可以使用SpVoice对象的Pause方法暂停语音播放使用Resume方法恢复播放使用Skip方法跳过当前正在播放的语音。 7. 监听语音事件:可以使用SpVoice对象的事件来实现对语音转换过程中的事件进行监听和处理。 通过以上步骤,就可以使用SpVoice实现文字到语音的转换。SpVoice提供了丰富的功能和灵活的接口,可以满足不同的应用需求,如语音合成、辅助阅读等。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值