在游戏中实现语音聊天和语音转化成文字

来源:互联网 发布:免费数据库 mysql p 编辑:程序博客网 时间:2024/04/29 10:46

今天碰到一个任务就是在游戏中添加语音聊天功能,并且需要将语音转化成文本信息,在聊天界面显示。
当然这种功能专业性太强,我们就只能选择使用第三方SDK来实现了。
国内做的语音识别比较好的就是科大讯飞,不过由于讯飞有没直接提供unity的解决方案,需要自己封装,所以我使用了亲加的unity SDK。
亲加集成了讯飞的语音识别功能,并且提供了云服务器来作为聊天服,具有加好友,私聊,聊天室,群等功能。
这里简单说下如何使用它的unity SDK。
首先是上官网下载相关的unity package:
http://www.gotye.com.cn/docs/ime/unity3d.html

然后在unity中导入包。
之后根据官网的文档,新建一个文本挂在在场景的任意一个GameObject上,添加代码如下:

using UnityEngine;using System.Collections;using System.Collections.Generic;using gotye;public class ChatMgr : GotyeMonoBehaviour  {    //这是在官网登记应用时获得的appKey     public string appKey = "apcb956c-2fe0-28ac-cb8-8ca52fc5f265";    public string packageName = "com.gotyeapi";    public string userName;    void Awake()    {          InvokeRepeating("mainLoop", 0.0f, 0.050f);        api.Init(appKey, packageName);        api.InitIflySpeechRecognition();        GotyeStatusCode code = api.Login(userName, null);    }       void mainLoop()    {          GotyeAPI.GetInstance().MainLoop();      }}

InvokeRepeating这个方法是用来开启信息监听的,照着写就行了,关键是api.InitIflySpeechRecognition(); 在开始时调用这个方法之后,以后录下的音频文件中,就会自动解析,生成对应的文本,在message.Text中直接调用就可以取到了。
另外需要实现各种对应的Listener接口,来监听异步信息,比如要监听登陆之后的返回包,就需要实现:

using UnityEngine;using System.Collections;using System.Collections.Generic;using gotye;public class Login : GotyeMonoBehaviour, LoginListener{    void Start()    {        api.AddListener(this);    }    public void onLogin(GotyeStatusCode code, GotyeUser user)    {        Debug.Log("onLogin--" + user.Name);        api.ReqFriendList();        api.BeginReceiveOfflineMessage();        GotyeStatusCode code_room = api.ReqRoomList(0);    }    public void onLogout(GotyeStatusCode code)    { }    public void onReconnecting(GotyeStatusCode code, GotyeUser currentUser)    { }}

同样的,api.ReqFriendList(); api.BeginReceiveOfflineMessage();这两个方法也要用相应的Listener去监听异步调用,上面这个脚本和之前的一样,需要挂在在场景中的任意一个活动的GameObject上。

登陆时,使用任意用户名即可(这跟设定的验证机制有关,有开放,限定,第三方认证三种),密码为空。
用户是在登陆的时候设定的,但是聊天室就得在后台生成了,可以自己设定聊天室的名称和头像。

发送消息可以用如下的方式:

    GotyeMessage message = GotyeMessage.CreateTextMessage(api.GetLoginUser(), api.GetLocalRoomList()[0], info);    api.SendMessage(message);

GotyeMessage.CreateTextMessage()第一个参数是发送者,第二个是发送对象,第三个是发送信息。其中发送对象可以是任意一个在服务器中存在的用户,也可以是某个聊天室或者群,但都必须是继承于(GotyeChatTarget)的对象。如果是聊天室或者群的话,就是对所有在聊天室里的人广播。

语音消息有一个录制过程,用按键触发的话,类似下面:

EventTriggerListener.Get(recordBtn).onDown = (GameObject go) =>    {        GotyeStatusCode code = api.StartTalk(Room.curRoom, GotyeWhineMode.GotyeWhineModeDefault, false, 60 * 1000);        talkingImg.SetActive(true);    };EventTriggerListener.Get(recordBtn).onUp = (GameObject go) =>    {        GotyeStatusCode code = api.StopTalk();        talkingImg.SetActive(false);    };

同样的需要注册一个ChatListener:

public class Chat : GotyeMonoBehaviour, ChatListener {    public void onStartTalk(GotyeStatusCode code, GotyeChatTarget target, bool isRealTime)    {        Debug.Log("录音开始");    }    public void onStopTalk(GotyeStatusCode code, bool realtime, GotyeMessage message)    {        Debug.Log("录音结束");        //需要把转化成的语音文本存到额外信息中        byte[] extraData = Encoding.UTF8.GetBytes(message.Text);        message.PutExtraData(extraData);        api.SendMessage(message);    }    public void onReceiveMessage(GotyeMessage message)    {        Debug.Log("收到消息");        string text = "";        if (message.Type == GotyeMessageType.Audio)        {            byte[] extraData = message.GetExtraData();            if (extraData != null)                text = string.Format("[{0}]: {1}", message.Sender.Name, Encoding.UTF8.GetString(message.GetExtraData()));            EventTriggerListener.Get(btn).onClick = (GameObject go) =>            {                //下载语音文件,需要在onDownloadMediaInMessage中实现监听                api.DownloadMediaInMessage(message);            };        }        else if (message.Type == GotyeMessageType.Text)        {            text = string.Format("[{0}]: {1}", message.Sender.Name, message.Text);        }        Debug.Log("消息为:" + text);    }}

这里需要注意一点,就是由于之前使用了api.InitIflySpeechRecognition();所以这里在录制完之后,可以直接通过message.Text来获取语音转成的文本信息,但是在发送消息给别人的时候,这个message.Text就会为空(设计问题),这时候需要我们手动把message.Text存到message.ExtraData中,因为这个ExtraData在传递时会保留,然后我们在接受这个消息的时候,把message.ExtraData中的信息转成string,就能获得语音转化成的文本信息了。

0 0
原创粉丝点击