码迷,mamicode.com
首页 > 移动开发 > 详细

AndroidStudio->Jar->unity3d实现讯飞语音接入

时间:2018-08-12 15:42:41      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:obj   生成   分享图片   href   vat   代码   ima   name   into   

一.引用

[MemoryC] Unity3D调用Android功能与组件(八)——讯飞语音识别与合成

二.成功导出Jar

1.mylibrary中的目录结构

 技术分享图片

2.导出jar

task makeJar(type: Copy) {
//删除存在的
delete ‘build/libs/mytest.jar‘
//设置拷贝的文件
from(‘build/intermediates/packaged-classes/release/‘)
//打进jar包后的文件目录
into(‘build/libs/‘)
//将classes.jar放入build/libs/目录下
//include ,exclude参数来设置过滤
//(我们只关心classes.jar这个文件)
include(‘classes.jar‘)
//重命名
rename (‘classes.jar‘, ‘mytest.jar‘)
}
            makeJar.dependsOn(build)

注意:是mylibrary中的gradle.

技术分享图片

3.将生成的Jar文件导入u3d中,测试TestU2A()->成功!

三.添加并改写IflyVoicejava的代码

1.导入相关libs

技术分享图片

2.复制IfyVoiceJava.class中代码到mylibrary1.java中并改写相关函数

技术分享图片

3.重新makeJar并导入U3D中

四.实现更改讯飞语音voicer

 1.弃用VoiceStatic.cs,重新封装T2VStatic.CS

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class T2VStatic
{
    public static readonly T2VStatic instance = (T2VStatic)Activator.CreateInstance(typeof(T2VStatic));

    //AndroidJavaClass
    AndroidJavaClass jc;

    AndroidJavaObject jo;
    AndroidJavaObject jo1;

    public T2VStatic()
    {




#if UNITY_ANDROID
                //Initialize AndroidJavaClass(Please do not delete the commended codes for that those code are for test and check)
        jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");

        jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
        jo1 = new AndroidJavaObject("com.unity3d.player.mylibrary1", "5b508fb6", jo);

        //string str = SetVoicer(VoI);
        jo1.Call("setInitListener", new XfInitListener());
        jo1.Call("setTtsListener", new XfSynthesizerListener());
        //jo1.Call(methodName: "setRecognizerListener", args: new xfRecognizerListener());

        jo1.Call("initVoice");
        

        //startRecognize();
        //startSpeaking("你好,小鬼,,,哈哈哈哈!Good morning! Congratulations! HOooray!");







#endif
        }

    public void startSpeaking(int VoI,string text)
    {
        jo1.Call("setTtsParam", SetVoicer(VoI));
        jo1.Call("startSpeak", new AndroidJavaObject("java.lang.String", text));
    }

    public void startRecognize()
    {
        jo1.Call("startRecognize");
    }
    private string SetVoicer(int i)
    {
        string str = "";
        switch (i)
        {
            case 0:
                str = "xiaoyan";//青年女声,,中英文普通话

                break;

            case 1:
                str = "xiaoyu";//青年男声,,中英文普通话
                break;
            case 2:
                str = "catherine";//青年女生,,英文
                break;
            case 3:
                str = "henry";//青年男声,,英文
                break;
            case 4:
                str = "xiaofeng";//青年男生,,中英文普通话
                break;
            case 5:
                str = "xiaoxin";//童年男声,汉语普通话
                break;
            case 6:
                str = "nannan";//同年女声,汉语普通话
                break;
            case 7:
                str = "vils";//老年男声,汉语普通话
                break;

            default:
                str = "xiaoyan";
                break;



        }
        return str;

    }
}

2.调用示例:

string str = "欢迎来到XX乐园!";
        T2VStatic.instance.startSpeaking(6, str);

 完毕,谢谢!

AndroidStudio->Jar->unity3d实现讯飞语音接入

标签:obj   生成   分享图片   href   vat   代码   ima   name   into   

原文地址:https://www.cnblogs.com/guaike01/p/9462445.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!