Unity接入语音SDk 语音识别

关于语音识别的来说,我个人觉得云娃语音对Unity开发者更加有好一点,因为在支持上云娃语音的SDK有直接的Unity包

PS:习惯性按Ctrl+S 好难受

首先大家先去云娃语音的官网去下载Unity的支持包-----------云娃语音SDK

然后打开Demo场景,我们进行后面的操作和深度开发也是修改Demo.cs文件

下面我就来剖析一下这个Demo.cs脚本文件


下面就是Demo.cs脚本的所有代码----------》》》

using UnityEngine;
using System.Collections;
using System;
using YunvaIM;

public class Demo : MonoBehaviour
{
    private string sUserId = "123";
    private string labelText = "ssss";
    string filePath = "";
    private string recordPath = string.Empty;//返回录音地址
    private string recordUrlPath = string.Empty;//返回录音url地址
    public GUISkin guiSkin;

    void Start()
    {
        EventListenerManager.AddListener(ProtocolEnum.IM_RECORD_VOLUME_NOTIFY, ImRecordVolume);//录音音量大小回调监听
        int init = YunVaImSDK.instance.YunVa_Init(0, 100041, Application.persistentDataPath, false, false);
        if (init == 0)
        {
            Debug.Log("初始化成功...");
            labelText = "初始化成功...";
        }
        else
        {
            Debug.Log("初始化失败...");
            labelText = "初始化失败...";
        }
    }




    void OnGUI()
    {
        if (guiSkin != null)
        {
            GUI.skin = guiSkin;
        }
        GUI.Box(new Rect(10f, 10f, 340, 900f), "菜单");
        sUserId = GUI.TextField(new Rect(20f, 50f, 150f, 100f), sUserId);
        if (GUI.Button(new Rect(20f, 150f, 150f, 100f), "登录"))
        {
            string ttFormat = "{{\"nickname\":\"{0}\",\"uid\":\"{1}\"}}";
            string tt = string.Format(ttFormat, sUserId, sUserId);
            string[] wildcard = new string[2];
            wildcard[0] = "0x001";
            wildcard[1] = "0x002";
            YunVaImSDK.instance.YunVaOnLogin(tt, "100", wildcard, 0, (data) =>
            {
                if (data.result == 0)
                {
                    labelText = string.Format("登录成功,昵称:{0},用户ID:{1}", data.nickName, data.userId);
                    //YunVaImSDK.instance.RecordSetInfoReq(true);//开启录音的音量大小回调
                }
                else
                {
                    labelText = string.Format("登录失败,错误消息:{0}", data.msg);
                }
            });
        }
        if (GUI.Button(new Rect(20f, 250f, 150f, 100f), "开始录音"))
        {
            labelText = "正在录音中。。。。。。";
            filePath = string.Format("{0}/{1}.amr", Application.persistentDataPath, DateTime.Now.ToFileTime());
            Debug.Log("FilePath:" + filePath);
            YunVaImSDK.instance.RecordStartRequest(filePath, 1);
        }
        if (GUI.Button(new Rect(20f, 350f, 150f, 100f), "停止录音"))
        {
            labelText = "停止录音.........";
            YunVaImSDK.instance.RecordStopRequest((data1) =>
            {
                recordPath = data1.strfilepath;
                labelText = "停止录音返回:" + recordPath;

                Debug.Log("停止录音返回:" + recordPath);
            },
            (data2) =>
            {
                recordUrlPath = data2.fileurl;
                labelText = "上传返回:" + data2.fileurl;
                Debug.Log("上传返回:" + data2.fileurl);
            },
            (data3) =>
            {
                labelText = "识别返回:" + data3.text;
                Debug.Log("识别返回:" + data3.text);
            });
        }
        if (GUI.Button(new Rect(20f, 450f, 150f, 100f), "播放语音"))
        {
            labelText = "播放语音.........";
            string ext = DateTime.Now.ToFileTime().ToString();
            YunVaImSDK.instance.RecordStartPlayRequest(recordPath, "", ext, (data2) =>
                                                           {
                                                               if (data2.result == 0)
                                                               {
                                                                   Debug.Log("播放成功");
                                                                   labelText = "播放成功";
                                                               }
                                                               else
                                                               {
                                                                   Debug.Log("播放失败");
                                                                   labelText = "播放失败";
                                                               }
                                                           });
        }
        if (GUI.Button(new Rect(190f, 50f, 150f, 100f), "停止播放"))
        {
            labelText = "停止播放.........";
            Debug.Log("停止播放");
            YunVaImSDK.instance.RecordStopPlayRequest();
        }
        if (GUI.Button(new Rect(190f, 150f, 150f, 100f), "语音识别"))
        {
            labelText = "语音识别.........";
            Debug.Log("语音识别");
            string ext = DateTime.Now.ToFileTime().ToString();
            YunVaImSDK.instance.SpeechStartRequest(recordPath, ext, (data3) =>
                                                    {
                                                        if (data3.result == 0)
                                                        {
                                                            labelText = "识别成功,识别内容:" + data3.text;
                                                            //recordUrlPath = data3.url;
                                                        }
                                                        else
                                                        {
                                                            labelText = "识别失败,原因:" + data3.msg;
                                                        }
                                                    });
        }
        if (GUI.Button(new Rect(190f, 250f, 150f, 100f), "上传语音"))
        {
            Debug.Log("准备上传:" + recordPath);
            labelText = "准备上传:" + recordPath;
            string fileId = DateTime.Now.ToFileTime().ToString();
            YunVaImSDK.instance.UploadFileRequest(recordPath, fileId, (data1) =>
            {
                if (data1.result == 0)
                {
                    recordUrlPath = data1.fileurl;
                    Debug.Log("上传成功:" + recordUrlPath);
                    labelText = "上传成功:" + recordUrlPath;
                }
                else
                {
                    labelText = "上传失败:" + data1.msg;
                    Debug.Log("上传失败:" + data1.msg);
                }
            });
        }
        if (GUI.Button(new Rect(190f, 350f, 150f, 100f), "下载语音"))
        {
            labelText = "下载语音......";
            string DownLoadfilePath = string.Format("{0}/{1}.amr", Application.persistentDataPath, DateTime.Now.ToFileTime());
            Debug.Log("下载语音:" + DownLoadfilePath);
            string fileid = DateTime.Now.ToFileTime().ToString();
            YunVaImSDK.instance.DownLoadFileRequest(recordUrlPath, DownLoadfilePath, fileid, (data4) =>
                                                   {
                                                       if (data4.result == 0)
                                                       {
                                                           Debug.Log("下载成功:" + data4.filename);
                                                           labelText = "下载成功:" + data4.filename;
                                                       }
                                                       else
                                                       {
                                                           Debug.Log("下载失败:" + data4.msg);
                                                           labelText = "下载失败:" + data4.msg;
                                                       }
                                                   });

        }
        if (GUI.Button(new Rect(190f, 450f, 150f, 100f), "登出"))
        {
            YunVaImSDK.instance.YunVaLogOut();
        }
        GUI.Label(new Rect(400f, 10f, 500f, 30f), "返回提示");
        GUI.Label(new Rect(400f, 30f, 500f, 50f), labelText);
    }
    public void ImRecordVolume(object data)
    {
        ImRecordVolumeNotify RecordVolumeNotify = data as ImRecordVolumeNotify;
        Debug.Log("ImRecordVolumeNotify:v_volume=" + RecordVolumeNotify.v_volume);
    }

}




看到上面的都很混乱,那么我们就提取一下

大家呢就在Start方法里面 处理初始化 和 登入 操作

void Start()
    {
        EventListenerManager.AddListener(ProtocolEnum.IM_RECORD_VOLUME_NOTIFY, ImRecordVolume);//录音音量大小回调监听
        int init = YunVaImSDK.instance.YunVa_Init(0, 100041, Application.persistentDataPath, false, false);
        if (init == 0)
        {
            Debug.Log("初始化成功...");
            //labelText = "初始化成功...";
        }
        else
        {
            Debug.Log("初始化失败...");
            //slabelText = "初始化失败...";
        }
        string ttFormat = "{{\"nickname\":\"{0}\",\"uid\":\"{1}\"}}";
        string tt = string.Format(ttFormat, sUserId, sUserId);
        string[] wildcard = new string[2];
        wildcard[0] = "0x001";
        wildcard[1] = "0x002";
        YunVaImSDK.instance.YunVaOnLogin(tt, "100", wildcard, 0, (data) =>
        {
            if (data.result == 0)
            {
                Debug.Log("登录成功!");
            }
            else
            {
                Debug.Log("登录失败!");
            }
        });
    }
然后下面大家就可以把语音的录入和录音的返回放到按钮内部

比如--一个麦克风按钮  当按下并持续的时候开始录音   抬起的时候就上传录音 并处理返回的文字

PS:在上传的时候会有延迟  这个延迟嘛   那就加个动画啊  什么的  让用户感到高科技的

下面就是按钮的操作

public void Down()
    {
        //开始录音
        filePath = string.Format("{0}/{1}.amr", Application.persistentDataPath, DateTime.Now.ToFileTime());
        Debug.Log("FilePath:" + filePath);
        YunVaImSDK.instance.RecordStartRequest(filePath, 1);
    }
    public void Up()
    {
        //语音返回
        //labelText = "停止录音.........";
        YunVaImSDK.instance.RecordStopRequest((data1) =>
        {
            recordPath = data1.strfilepath;

            Debug.Log("停止录音返回:" + recordPath);
        },
        (data2) =>
        {
            recordUrlPath = data2.fileurl;
            Debug.Log("上传返回:" + data2.fileurl);
        },
        (data3) =>
        {
            text.text += data3.text + "\n\t";
            Debug.Log("识别返回:" + data3.text);
        });
    }
PS:Ctrl+S好难受
大家对照这个就可以把里面提供的所有功能全部拿出来了     

只有“《识别返回:》”后面的才是识别你说的话的文字  记得显示出来



欢迎大家在下面留言。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值