C#调用百度语音识别小Demo

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#调用百度语音识别小Demo相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Threading;
using Newtonsoft.Json.Linq;

namespace voice
{
    public partial class Form1 : Form
    {
        string testtoken = "";
        string cuid = "";//随便写
        string serverURL = "http://vop.baidu.com/server_api";
        public Form1()
        {
            InitializeComponent();
            cuid = Guid.NewGuid().ToString();
        }

        private string Post(string audioFilePath)
        {
            serverURL += "?lan=zh&cuid=kwwwvagaa&token=" + testtoken;
            FileStream fs = new FileStream(audioFilePath, FileMode.Open);
            byte[] voice = new byte[fs.Length];
            fs.Read(voice, 0, voice.Length);
            fs.Close();
            fs.Dispose();

            HttpWebRequest request = null;

            Uri uri = new Uri(serverURL);
            request = (HttpWebRequest)WebRequest.Create(uri);
            request.Timeout = 5000;
            request.Method = "POST";
            request.ContentType = "audio/wav; rate=16000";
            request.ContentLength = voice.Length;
            try
            {
                using (Stream writeStream = request.GetRequestStream())
                {
                    writeStream.Write(voice, 0, voice.Length);
                    writeStream.Close();
                    writeStream.Dispose();
                }
            }
            catch
            {
                return null;
            }
            string result = string.Empty;
            string result_final = string.Empty;
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8))
                    {
                        string line = string.Empty;
                        StringBuilder sb = new StringBuilder();
                        while (!readStream.EndOfStream)
                        {
                            line = readStream.ReadLine();
                            sb.Append(line);
                            sb.Append("\r");
                        }
                        readStream.Close();
                        readStream.Dispose();
                        result = sb.ToString();
                        string[] indexs = result.Split(‘,‘);
                        foreach (string index in indexs)
                        {
                            string[] _indexs = index.Split(‘"‘);
                            if (_indexs[2] == ":[")
                                result_final = _indexs[3];
                        }
                    }
                    responseStream.Close();
                    responseStream.Dispose();
                }
                response.Close();
            }
            return result_final;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = Post(Application.StartupPath + "\\2.wav");
        }
    }
}

附件中有源代码,具体看里面的README

以上是关于C#调用百度语音识别小Demo的主要内容,如果未能解决你的问题,请参考以下文章

百度语音识别REST API完整Demo

Python调用百度接口(情感倾向分析)和讯飞接口(语音识别关键词提取)处理音频文件

百度语音识别TTS REST API 用C# 怎么处理下行数据 求实例

Android集成讯飞语音百度语音阿里语音识别

小程序语音红包中遇到的 语音识别silk转wav格式 如何在线转 或者mp3转wav格式

如何用python调用百度语音识别