下面是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. netperf

    官网 下载 装包 yum -y install make automake libtool pkgconfig libaio-devel 编译安装 ./autogen.sh ./configure m ...

  2. 【mac微信小助手】WeChatPlugin使用教程!

    微信小助手 mac版集微信防撤回和微信多开等诸多功能于一身,可以有效的阻止朋友微信撤回消息,还能开启无手机验证登录,再也不用每次登录扫码验证啦,非常方便!   wechatplugin mac版安装教 ...

  3. unity 根据平板 或者 手机 确立横竖屏

    /* ######### ############ ############# ## ########### ### ###### ##### ### ####### #### ### ####### ...

  4. linux基础之加密解密、PKI及SSL、创建私有CA

    加密解密基础 1. 对称加密: 加密和解密使用同一个密钥 常见的加密算法有:DES.3DES.AES.Blowfish.Twofish.IDEA.RC6.CAST5 特性: 1. 加密.解密使用同一个 ...

  5. xueping wang 记录

    https://www.bbsmax.com/A/lk5aVBod1O/ https://pkgs.org/statistics/ 在firefox的调试控制台, 下面有一个独立的分割的控制台窗口, ...

  6. CAS工程用redis集群存储票据ticket Spring整合

    maven jar包版本: <dependency> <groupId>redis.clients</groupId> <artifactId>jedi ...

  7. 前端学习之HTML

    HTML介绍 Web服务本质 import socket sk = socket.socket() sk.bind(("127.0.0.1", 8080)) sk.listen(5 ...

  8. redis 的简单使用

    Redis是什么:内存型数据库,内存取数据与db硬盘取数据.......速度没得比,啥 内存,我直接创建变量就OK了嘛,用redis 干嘛,抱歉我只会开车,无法解答. 为什么使用  :在一些高并发业务 ...

  9. vue数据变动监测

    原文链接:https://blog.csdn.net/man_tutu/article/details/72148362 对象: 不能监测到: var vm = new Vue({ data:{ a: ...

  10. docker笔记(2)-----容器连接

    2019-01-12  13:57:36 Dockerfile基本结构: 基础镜像内容:FROM指明base image 维护者信息: 镜像操作指令:RUN指令,每运行一条RUN指令,镜像添加新的一层 ...