最近在做微软的一点小项目需要用到语音识别,但是微软的语音识别真的太不给力了,其中遇到很多麻烦。偶然听到大熊说google的语音识别接口,于是搜索资料弄了一个,整理出来,希望能帮助需要的朋友,大神别喷就好。

一、使用Google Speech  API

思路解析:   1、首先通过WPF录音,这里注意码率必须为16000。

2、得到wav格式的录音文件流

3、将该录音文件流传给google的语音识别接口 http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=zh-CN

4、解析google识别出来的文字信息。

先看如下参考资料 :

http://www.cnblogs.com/onlytiancai/archive/2008/08/02/p2p_sound_chat.html

http://blog.csdn.net/dlangu0393/article/details/7214728

http://www.cnblogs.com/eboard/archive/2012/02/29/speech-api.html

我的程序中引用的是 蛙蛙池塘 的第一个附件中的dll,把client中的dll引进项目即可实现录音。

附上我的代码:

MainWindow.xaml

<Window x:Class="SoundRecord.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        Closing="Window_Closing_1">
    <StackPanel Orientation="Vertical">
        <TextBlock Name="statusBlock" FontSize="30" Foreground="Red" Text="还未开始..."/>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
            <Button Name="StartBtn" Width="200" Height="200" Background="Red" Content="开始" FontSize="40" Click="StartBtn_Click_1"/>
            <Button Name="StopBtn" Width="200" Height="200" Background="Red" Content="结束" FontSize="40" Click="StopBtn_Click_1"/>
        </StackPanel>
    </StackPanel>
</Window>

MainWindow.cs

using Microsoft.DirectX.DirectSound;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WawaSoft.Media;
namespace SoundRecord
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
        CaptureSound record = null;
        private int fileIndex = 0;
        public MainWindow()
            InitializeComponent();
            WaveFormat format = DirectSoundManager.CreateWaveFormat(16000, 16, 1);
            record = new CaptureSound(format);
        private void StartBtn_Click_1(object sender, RoutedEventArgs e)
            statusBlock.Text = "录音中...";
            record.FileName = "F:\\hello-" + fileIndex + ".wav";
            record.Start();
        private void StopBtn_Click_1(object sender, RoutedEventArgs e)
            statusBlock.Text = "录音停止...";
            record.Stop();
            Thread thread = new Thread(new ThreadStart(GoogleSTT));
            thread.Start();
        /// <summary>
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Closing_1(object sender, System.ComponentModel.CancelEventArgs e)
            MessageBox.Show("Closing App");
            record = null;
        delegate void MyDelegate();
            /// <summary>
        /// 调用GOOLE语音识别引擎
        /// </summary>
        /// <returns></returns>
        private void GoogleSTT()
            MyDelegate dl = new MyDelegate(delegate() { statusBlock.Text = "语音请求中"; });
            Dispatcher.BeginInvoke(dl);
            Console.WriteLine("语音请求中...");
            string result = string.Empty;
                string inFile = record.FileName;
                FileStream fs = new FileStream(inFile, FileMode.Open);
                byte[] voice = new byte[fs.Length];
                fs.Read(voice, 0, voice.Length);
                fs.Close();
                HttpWebRequest request = null;
                string url = "http://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=zh-CN";
                Uri uri = new Uri(url);
                request = (HttpWebRequest)WebRequest.Create(uri);
                request.Method = "POST";
                request.ContentType = "audio/L16; rate=16000";
                request.ContentLength = voice.Length;
                using (Stream writeStream = request.GetRequestStream())
                    writeStream.Write(voice, 0, voice.Length);
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    using (Stream responseStream = response.GetResponseStream())
                        using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8))
                            result = readStream.ReadToEnd();
                            Console.WriteLine("语音解析 :" + result);
            catch (Exception ex)
                Console.WriteLine(ex.StackTrace);
            //return result;
 
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>

修改后会出现错误:什么LoaderLock 之类的。
翻译成中文是 : 正试图在 os 加载程序锁内执行托管代码。不要尝试在 DllMain 或映像初始化函数内运行托管代码,这样做会导致应用程序挂起。

问题的解决方法:

这个问题只需要把vs2005菜单的 调试->异常->Managed Debuggin Assistants->LoaderLock 的选中状态去掉即可!如果异常(exception)这一项没有的话,在工具---自定义---命令选项卡---左边选择调试--右边把异常托到菜单里就可以了。异常也有个快捷键Ctrl+Alt+E.

丑陋的测试界面,点击开始后开始录音,录完后点击结束即可上传数据给google,然后等待控制台输出结果即可,这里并没有进行json解析,需要的朋友自己解析吧。

这里例子的代码在这里 : http://download.csdn.net/detail/bboyfeiyu/5165924

二、使用微软的语音识别接口

直接上代码了。

using System;
using System.Speech.Recognition;
namespace SpeechRecognitionApp
  class Program
    static void Main(string[] args)
      // 需要使用中文语音识别的话把en-US改为zh-CN即可,不过微软的特点就是不准
      using (
      SpeechRecognitionEngine recognizer =
        new SpeechRecognitionEngine(
          new System.Globalization.CultureInfo("en-US")))
        // 载入语法,这里是没有载入自定义的语法。你也可以设定语法,可以指定一些命令之类的
        recognizer.LoadGrammar(new DictationGrammar());
        /* 自己添加语法,在colorChoice 里面增加即可,具体要根据你需要的来设定。
        Choices colorChoice = new Choices(new string[] { "red", "green", "blue" });
        GrammarBuilder colorElement = new GrammarBuilder(colorChoice);
        Grammar grammar = new Grammar(colorElement);
        grammar.Enabled = true;
        // Create and load a dictation grammar.
        recognizer.LoadGrammar(grammar); 
        // 增加事件处理
        recognizer.SpeechRecognized += 
          new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
        // Configure input to the speech recognizer.
        recognizer.SetInputToDefaultAudioDevice();
        // Start asynchronous, continuous speech recognition. 启动语音识别
        recognizer.RecognizeAsync(RecognizeMode.Multiple);
        // Keep the console window open.
        while (true)
          Console.ReadLine();
    // Handle the SpeechRecognized event.    语音识别事件处理,获得识别到的文本。
    static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
      Console.WriteLine("Recognized text: " + e.Result.Text);

参考资料(MSDN):

http://msdn.microsoft.com/zh-cn/library/system.speech.recognition.speechrecognitionengine.aspx

结束语: 希望能够帮到需要的人吧,有什么错误大神指出,别喷就好。

开发环境: Windows 7工具 : VS2012前言 : 最近在做微软的一点小项目需要用到语音识别,但是微软的语音识别真的太不给力了,其中遇到很多麻烦。偶然听到大熊说google的语音识别接口,于是搜索资料弄了一个,整理出来,希望能帮助需要的朋友,大神别喷就好。一、使用Google Speech  API思路解析:   1、首先通过WPF录音,这里注意码率必
Naudio是集录音、播放的源码库。https://github.com/naudio/NAudio,这是基于.net的框架。 在这链接的源码里有各种.net 框架的例子,对于基于.net wpf/universal 的程序,还提供了波形图的绘制。 我从没接触过音频类的技术,有个基于wpf 的项目需要快速提供实时绘制录音的图形。 因为源码库提供的wpf例子是基于文件播放的,其还中包括了比较多参数设置,技术上用了很多反射。很不直观 ,又因为对音频api毫无概念,想快速修改为基于实时录音还是需要费点时间的。
HTML5录音借鉴的网上的代码,但是下载下来却无法用,查阅了好多资料,终于在国外某网站上找到原因,原来是js函数废弃了,替换为新的js函数名即可。 HTML5录音的代码:http://www.it165.net/design/html/201406/2651.html 需修改的部分: var HZRecorder = function (stream, config) {
最近需要给自己开发的软件添加语音导航功能,百度+摸索终于实现了,不过比较简陋,分享出来供大家娱乐批评! 现在市场上的语音引擎不多,有微软SAPI、googgle、讯飞,经过多番比较最终决定使用微软SAPI 5.1,原因是免费,便于我集成到winform程序里。 首先,要实现微软的这个语音识别需要几个东西: 1.安装微软SAPI 5.1 SDK 2.安装中文语言包 这两个东西网上都可以下载
首先,语音识别技术已经不是什么新鲜的词汇了,各大公司也提供了自己的语音识别API,据说百度、讯飞等公司的识别率已经达到99%。 最近我也想给网站加上一个语音识别功能,用于搜索词汇。我首选的是讯飞,毕竟人家是专业做语音的,但关于html5的SDK讯飞已经下架,无法使用人家现成的接口。 没办法只能使用百度的语音识别,百度语音识别,需要提供音频文件,格式为pcm、wav 、avr。所以需要做一个ht...
在System.Speech程序集中,微软增加了一些非常酷的东西:语音合成,将文本转换为语音的能力,以及语音识别,将语音识别成文本的能力。 我们将专注于本文中的语音合成,然后在下一章进行语音识别。 要将文本转换为语音,我们将使用SpeechSynthesizer类。 这个类在System.Speech程序集中,我们需要添加它以在我们的应用程序中使用它。 根据您使用的Visual Studio版本,该过程如下所示: 添加适当的程序集后,我们现在可以使用System.Speech.Synthesis
在网上查了好久,后来终于用一位大佬的源码搞了出来,经这位大佬的同意,我把这个demo放上来,以方便以后有同样需求的人能够轻松的解决掉这个问题,不要像我一样这么苦呵呵的。 大佬的代码是winform,我放到了wpf上,代码都是一样的 MainWindow.xaml <Window x:Class="TingXieB.MainWindow" xmlns="h...