下面是API类 
Asr.cs

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine; /// <summary>
/// 用户解析token的json数据
/// </summary>
class TokenResponse
{
public string access_token = null;
} public class Asr
{
public string SecretKey { get; private set; } public string APIKey { get; private set; }
public string Token { get; private set; } public Asr(string apiKey, string secretKey)
{
APIKey = apiKey;
SecretKey = secretKey;
} public IEnumerator GetAccessToken()
{
var uri =
string.Format(
"https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id={0}&client_secret={1}",
APIKey, SecretKey);
var www = new WWW(uri);
yield return www; if (string.IsNullOrEmpty(www.error))
{
var result = JsonUtility.FromJson<TokenResponse>(www.text);
Token = result.access_token;
Debug.Log("Get access_token successfully");
}
else
{
Debug.LogError(www.error);
}
} public IEnumerator Recognize(byte[] data, Action<string> callback)
{
var uri =
string.Format("http://vop.baidu.com/server_api?lan=zh&cuid={0}&token={1}",
SystemInfo.deviceUniqueIdentifier, Token); var headers = new Dictionary<string, string> { { "Content-Type", "audio/pcm;rate=16000" } }; var www = new WWW(uri, data, headers);
yield return www;
if (string.IsNullOrEmpty(www.error))
{
Debug.Log(www.text); callback(www.text);
}
else
Debug.LogError(www.error);
} /// <summary>
/// 将Unity的AudioClip数据转化为PCM格式16bit数据
/// </summary>
/// <param name="clip"></param>
/// <returns></returns>
public static byte[] ConvertAudioClipToPCM16(AudioClip clip)
{
var samples = new float[clip.samples * clip.channels];
clip.GetData(samples, );
var samples_int16 = new short[samples.Length]; for (var index = ; index < samples.Length; index++)
{
var f = samples[index];
samples_int16[index] = (short)(f * short.MaxValue);
} var byteArray = new byte[samples_int16.Length * ];
Buffer.BlockCopy(samples_int16, , byteArray, , byteArray.Length); return byteArray;
}
}

下面是测试类

main.cs

using UnityEngine;
using System.Collections;
using UnityEngine.UI; public class main : MonoBehaviour { public string APIKey = "";
public string SecretKey = "";
public Button StartButton;
public Button StopButton;
public Text DescriptionText; private AudioClip _clipRecord = new AudioClip();
private Asr _asr; void Start()
{
_asr = new Asr(APIKey, SecretKey);
StartCoroutine(_asr.GetAccessToken()); StartButton.gameObject.SetActive(true);
StopButton.gameObject.SetActive(false);
DescriptionText.text = ""; StartButton.onClick.AddListener(OnClickStartButton);
StopButton.onClick.AddListener(OnClickStopButton);
} private void OnClickStartButton()
{
StartButton.gameObject.SetActive(false);
StopButton.gameObject.SetActive(true);
DescriptionText.text = "Listening..."; _clipRecord = Microphone.Start(null, false, , );
} private void OnClickStopButton()
{
StartButton.gameObject.SetActive(false);
StopButton.gameObject.SetActive(false);
DescriptionText.text = "Recognizing...";
Microphone.End(null);
Debug.Log("end record");
var data = Asr.ConvertAudioClipToPCM16(_clipRecord);
StartCoroutine(_asr.Recognize(data, s =>
{ DescriptionText.text = s;
StartButton.gameObject.SetActive(true);
})); }
}

资源来源于关尔Manic的技术园

http://blog.csdn.net/zhenghongzhi6/article/details/78688571#comments

Unity中使用百度中文语音识别功能的更多相关文章

  1. 利用HBuilder开发基于MUI的H5+ app中使用百度地图定位功能

    定位功能有两种方法: 首先要初始化内置地图: var map = new plus.maps.Map("map"); 这里黄色的map是html里面的id: <div id= ...

  2. Android 百度地图开发(一)--- 申请API Key和在项目中显示百度地图

      标签: Android百度地图API Key  分类: Android 百度地图开发(2)    最近自己想研究下地图,本来想研究google Map,但是申请API key比较坑爹,于是从百度地 ...

  3. 百度语音识别REST API——通过使用Http网络请求方式获得语音识别功能

    百度语音识别通过REST API的方式给开发人员提供一个通用的HTTP接口,基于该接口,开发人员能够轻松的获取语音识别能力,本文档描写叙述了使用语音识别服务REST API的方法. 长处: 较之开发人 ...

  4. iOS中 语音识别功能/语音转文字教程详解 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 原文地址:http://blog.csdn.net/qq_31810357/article/details/5111 ...

  5. iOS中 语音识别功能/语音转文字教程具体解释 韩俊强的博客

    原文地址:http://blog.csdn.net/qq_31810357/article/details/51111702 前言:近期研究了一下语音识别,从百度语音识别到讯飞语音识别:首先说一下个人 ...

  6. AngularJS进阶(十九)在AngularJS应用中集成百度地图实现定位功能

    在AngularJS应用中集成百度地图实现定位功能 注:请点击此处进行充电! 前言 根据项目需求,需要实现手机定位功能,考虑到百度业务的强大能力,遂决定使用百度地图第三方服务. 添加第三方模块的步骤与 ...

  7. Unity中使用WebView

    Unity中使用WebView @(设计) 需求,最近游戏中需要引入H5直播页面和更新比较频繁的赛事页面,需求包括:加密传参数.和Unity交互,在Unity框架下其实有几种方案: 内置函数Appli ...

  8. 3D语音天气球(源码分享)——在Unity中使用Android语音服务

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 开篇废话: 这个项目准备分四部分介绍: 一:创建可旋转的"3D球":3 ...

  9. Unity编程标准导引-Unity中的基本概念-2.1界面概览

    Unity中的基本概念 本文我们介绍Unity中的基本概念,包括:场景.游戏对象.组件.预制件.资源等. 2.1.界面概览 打开Unity之后,我们大概可以看到以上画面,以上画面中即显示了我们最常用到 ...

随机推荐

  1. 快速安装 Laravel 5.7

    $ git clone https://github.com/laravel/laravel.git $ cd laravel $ cp .env.example .env $ composer in ...

  2. 输入时间参数获取rds备份集信息

    1.脚本 [root@localhost tmp]# more geturl_test.py #!/usr/bin/env python #coding=utf- import os, json, u ...

  3. mysqli_query($conn, $sql)的返回值类型

    SQL语句的分类: DDL: Data Define Language,数据定义语言——定义列 CREATE / DROP / ALTER / TRUNCATE DML: Data Manipulat ...

  4. Codeforces Round #503 (by SIS, Div. 2)

    连接:http://codeforces.com/contest/1020 C.Elections 题型:你们说水题就水题吧...我没有做出来...get到了新的思路,不虚.好像还有用三分做的? KN ...

  5. Native App自动化测试及Appium框架介绍

    一  自动化工具简介 1 Appium : 开源,跨平台的自动化测试工具,用于测试Native(原生)和Hybrid(混合)应用,支持IOS/Android/FirefoxOS 平台. 2  环境 : ...

  6. spring boot 2使用Mybatis多表关联查询

    模拟业务关系:一个用户user有对应的一个公司company,每个用户有多个账户account. spring boot 2的环境搭建见上文:spring boot 2整合mybatis 一.mysq ...

  7. 论文笔记:Fast Neural Architecture Search of Compact Semantic Segmentation Models via Auxiliary Cells

    Fast Neural Architecture Search of Compact Semantic Segmentation Models via Auxiliary Cells 2019-04- ...

  8. 【HNOI 2016】网络

    Problem Description 一个简单的网络系统可以被描述成一棵无根树.每个节点为一个服务器.连接服务器与服务器的数据线则看做一条树边.两个服务器进行数据的交互时,数据会经过连接这两个服务器 ...

  9. yaf框架在windows上的环境配置和安装

    1.首先检测你的php版本 如图:Architecture:×86和thread Safety:disabled 这个有什么用呢? 2.进入这个网站 tgz是linux下的扩展包,windows下点D ...

  10. selenium+java 数据驱动

    一.数据驱动测试概念 数据驱动测试是相同的测试脚本使用不同的测试数据执行,测试数据和测试行为完全分离. 二.实施数据驱动测试的步骤: 1.编写测试脚本,脚本需要支持程序对象.文件或者数据库读入测试数据 ...