打开andoridstudio新建工程。修改包名等,然后选择EmptyActivity。新建libs导入unity和讯飞jar和库
然后在MainActivity里写代码
public class MainActivity extends UnityPlayerActivity {
private static final int REQUEST_CODE_READ_EXTERNAL_STORAGE_PERMISSIONS = 1;
private static final int REQUEST_CODE_WRITE_EXTERNAL_STORAGE = 2;
//跳转安卓wifi设置
public void SetWifi(){
Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
startActivity(intent);
}
//跳转安卓麦克风设置
public void SetMic(){
Uri packageURI = Uri.parse("package:" + "com.shuoshuoshuo.englishtongue");
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,packageURI);
startActivity(intent);
}
//设置测评文本,如果包含空格则测评句子否则测评单词
public void SetevaText(String strEnglish)
{
if(strEnglish.contains(" "))
{
mEvaText = strEnglish;
}
else
{
mEvaText = "[word]\n" + strEnglish;
}
}
public void StartReco()
{
// 设置参数
setParams();
int ret = mIse.startEvaluating(mEvaText, null, mEvaluatorListener);
if (ret != ErrorCode.SUCCESS)
{
InvokeUnity_Error("评估失败,错误码:" + ret);
return ;
}
InvokeUnity_Tip("RecoStart");
}
public void StopReco()
{
if (mIse.isEvaluating()) {
mIse.stopEvaluating();
}
InvokeUnity_Tip("RecoStop");
}
public void CancelReco()
{
mIse.cancel();
InvokeUnity_Tip("RecoCancel");
}
private IWXAPI api;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
InitEngine();
}
protected void onDestroy() {
super.onDestroy();
if (null != mIse) {
mIse.destroy();
mIse = null;
}
}
@Override
protected void onResume() {
// 开放统计 移动数据统计分析
FlowerCollector.onResume(MainActivity.this);
FlowerCollector.onPageStart(TAG);
super.onResume();
}
@Override
protected void onPause() {
// 开放统计 移动数据统计分析
FlowerCollector.onPageEnd(TAG);
FlowerCollector.onPause(MainActivity.this);
super.onPause();
}
private SpeechEvaluator mIse;
private String mEvaText= "Swim away.";
// 评测语种
private String language;
// 评测题型
private String category;
// 结果等级
private String result_level;
private final static String PREFER_NAME = "ise_settings";
private static String TAG = MainActivity.class.getSimpleName();
private void InitEngine()
{
SpeechUtility.createUtility(MainActivity.this, "appid=" + getString(R.string.app_id));//讯飞appid
mIse = SpeechEvaluator.createEvaluator(MainActivity.this, null);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(
new String[] { Manifest.permission.READ_EXTERNAL_STORAGE },
REQUEST_CODE_READ_EXTERNAL_STORAGE_PERMISSIONS);
requestPermissions(
new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },
REQUEST_CODE_WRITE_EXTERNAL_STORAGE);
}
}
}
private void setParams()
{
SharedPreferences pref = getSharedPreferences(PREFER_NAME, MODE_PRIVATE);
// 设置评测语言
language = pref.getString(SpeechConstant.LANGUAGE, "en_us");
// 设置需要评测的类型
if(mEvaText.contains("[word]"))
category = pref.getString(SpeechConstant.ISE_CATEGORY, "read_word");
else
category = pref.getString(SpeechConstant.ISE_CATEGORY, "read_sentence");
// 设置结果等级(中文仅支持complete)
result_level = pref.getString(SpeechConstant.RESULT_LEVEL, "plain");//complete
// 设置语音前端点:静音超时时间,即用户多长时间不说话则当做超时处理
String vad_bos = pref.getString(SpeechConstant.VAD_BOS, "4000");
// 设置语音后端点:后端点静音检测时间,即用户停止说话多长时间内即认为不再输入, 自动停止录音
String vad_eos = pref.getString(SpeechConstant.VAD_EOS, "500");
// 语音输入超时时间,即用户最多可以连续说多长时间;
String speech_timeout = pref.getString(SpeechConstant.KEY_SPEECH_TIMEOUT, "5000");
mIse.setParameter(SpeechConstant.LANGUAGE, language);
mIse.setParameter(SpeechConstant.ISE_CATEGORY, category);
mIse.setParameter(SpeechConstant.TEXT_ENCODING, "utf-8");
mIse.setParameter(SpeechConstant.VAD_BOS, vad_bos);
mIse.setParameter(SpeechConstant.VAD_EOS, vad_eos);
mIse.setParameter(SpeechConstant.KEY_SPEECH_TIMEOUT, speech_timeout);
}
// 评测监听接口
private EvaluatorListener mEvaluatorListener = new EvaluatorListener() {
@Override
public void onResult(EvaluatorResult result, boolean isLast) {
Log.d(TAG, "evaluator result :" + isLast);
if (isLast) {
StringBuilder builder = new StringBuilder();
builder.append(result.getResultString());
if (!TextUtils.isEmpty(builder)) {
XmlResultParser resultParser = new XmlResultParser();
Result data = resultParser.parse(builder.toString());
Float i = data.total_score;
InvokeUnity_RecoLast(i.toString());
}
else
{
InvokeUnity_RecoLast("0");
}
}
else
{
InvokeUnity_Reco("0");
}
}
@Override
public void onError(SpeechError error) {
if(error != null) {
InvokeUnity_Error("error:"+ error.getErrorCode() + "," + error.getErrorDescription());
} else {
Log.d(TAG, "evaluator over");
}
}
@Override
public void onBeginOfSpeech() {
// 此回调表示:sdk内部录音机已经准备好了,用户可以开始语音输入
Log.d(TAG, "evaluator begin");
InvokeUnity_RecognizerListener("onBeginOfSpeech");
}
@Override
public void onEndOfSpeech() {
// 此回调表示:检测到了语音的尾端点,已经进入识别过程,不再接受语音输入
Log.d(TAG, "evaluator stoped");
InvokeUnity_RecognizerListener("onEndOfSpeech");
}
@Override
public void onVolumeChanged(int volume, byte[] data) {
Log.d(TAG, "返回音频数据:"+data.length);
Integer i = volume;
InvokeUnity_Volume(i.toString());
}
@Override
public void onEvent(int eventType, int arg1, int arg2, Bundle obj) {
// 以下代码用于获取与云端的会话id,当业务出错时将会话id提供给技术支持人员,可用于查询会话日志,定位出错原因
// if (SpeechEvent.EVENT_SESSION_ID == eventType) {
// String sid = obj.getString(SpeechEvent.KEY_EVENT_SESSION_ID);
// Log.d(TAG, "session id =" + sid);
// }
}
};
/* 定义一个调用Unity方法的方法 ,基于UnitySendMessage实现 。
* 由于没有布局文件,我们在这里通过Unity调用这个方法*/
//发送每个阶段提示
private void InvokeUnity_Tip(String mStr)
{
UnityPlayer.UnitySendMessage("Unity2Android", "SetMessageTip", mStr);
}
//发送错误信息
private void InvokeUnity_Error(String mStr)
{
UnityPlayer.UnitySendMessage("Unity2Android", "SetMessageError", mStr);
}
//特殊录制结果
private void InvokeUnity_Reco(String mStr)
{
UnityPlayer.UnitySendMessage("Unity2Android", "RecordResult", mStr);
}
//录制结果
private void InvokeUnity_RecoLast(String mStr)
{
UnityPlayer.UnitySendMessage("Unity2Android", "RecordResultLast", mStr);
}
//录制时候获取麦克风音量
private void InvokeUnity_Volume(String mStr)
{
UnityPlayer.UnitySendMessage("Unity2Android", "RecordVolume", mStr);
}
//录制过程,用于获取开始录音和结束录音
private void InvokeUnity_RecognizerListener(String mStr)
{
UnityPlayer.UnitySendMessage("Unity2Android", "RecordRoutine", mStr);
}