65.9K
CodeProject 正在变化。 阅读更多。
Home

在设备事件上启动语音识别

2012 年 12 月 6 日

CPOL

1分钟阅读

viewsIcon

17237

在设备事件上启动语音识别。

 

我最近刚加入 Plantronics(缤特力),并且不久前开始学习 Spokes SDK

为了展示它使用起来有多么简单,并让大家了解可以完成多少有趣的事情,我给自己设定了一个挑战,即构建这个简单的插件,当耳机戴上时(设备事件 Don)启动 Windows 语音识别工具,当耳机摘下时(设备事件 Doff)停止应用程序。

这是我的 IPlugin 实现(基本上基于示例代码,并清理后仅使用所需内容)

using System;
using System.Windows.Forms;
using System.Threading;
using Plantronics.Device.Common;
using Plantronics.UC.Common;
using System.Diagnostics;
namespace Plantronics.Plugin.Sample.SpeechReco
{
 public class SpeechRecoPlugin : IPlugin
 {
        ISession m_session = null;
        IDevice m_device = null;

       
        public string Name
        {
            get { return "Plantronics SDK Speech Recognition Plugin"; }
        }
        
        public bool Init()
        {
            m_session = SessionManager.Instance.Register(Name);
            if (m_session != null)

            {
                m_device = m_session.ActiveDevice;
                if (m_device != null)
                {
                    // looking for Don and Doff only
                    // Don - start windows speech recognition app
                    // Doff - exits the app
                    m_device.DeviceListener.HeadsetStateChanged += new DeviceListenerEventHandler(
                        DeviceListener_HeadsetStateChanged);
                }

            }
            return true;
        }
         // Exit code
        public void Exit()
        {   
            if (m_device != null)
            {

                m_device.DeviceListener.HeadsetStateChanged -= DeviceListener_HeadsetStateChanged;
            }
            SessionManager.Instance.UnRegister(m_session);
        }
        #region DeviceListener events
        void DeviceListener_HeadsetStateChanged(object sender, DeviceListenerEventArgs e)
        {
            if (e.HeadsetStateChange == HeadsetStateChange.Don)
            {

                // check if speech reco tool is already running
                Boolean isSapiRunning = false;
                foreach (Process clsProcess in Process.GetProcesses())
                {
                    if (clsProcess.ProcessName.Contains("sapisvr"))
                    {
                        isSapiRunning = true;
                        break;
                    }

                }
                // start msft speech reco tool
                if (!isSapiRunning)
                {
                    Process p = Process.Start(@"C:\Windows\Speech\Common\sapisvr.exe",
                        @"-SpeechUX");
                }
            }
            else if (e.HeadsetStateChange == HeadsetStateChange.Doff)
            {

                foreach (Process clsProcess in Process.GetProcesses())
                {
                    if (clsProcess.ProcessName.Contains("sapisvr"))
                    {
                        clsProcess.CloseMainWindow();
                        break;
                    }
                }
            }

        }
 
        #endregion DeviceListener events
 }
}

相同的代码可以用来启动和停止任何其他应用程序,并且可以使用其他事件来自动化您电脑上的其他任务。

以下是微软关于语音识别的一些提示

http://windows.microsoft.com/en-US/windows7/What-can-I-do-with-Speech-Recognition

我强烈建议(这真的必须做)将您的耳机设置为用于语音识别的麦克风。

http://windows.microsoft.com/en-US/windows7/Set-up-your-microphone-for-Speech-Recognition

希望您喜欢,并随时向我们反馈您使用 SDK 完成的各种其他有趣的事情。

本文由 Ricardo de Andrade 撰写。Ricardo 是 Plantronics 的系统架构师和推广者,致力于帮助开发者社区、客户和合作伙伴使用 Spokes SDK 并构建围绕当前和未来产品解决方案。Ricardo 在软件和云分布式架构方面拥有丰富的经验,尤其是在电信领域。Ricardo 此前曾在微软工作,帮助客户和合作伙伴开发基于云的语音识别应用程序,并将他们的 Web 服务集成到 Microsoft Tellme 服务中。

© . All rights reserved.