Unity获取音筒的声音及实现吹气效果
发表于2018-01-05
如何通过获取音筒的声音实现实现吹气效果?下面给大家准备了两个脚本,帮助大家去实现。
Unity这边一个脚本:
using UnityEngine; using UnityEngine.UI; using System.Collections; public class RecognizeManager : MonoBehaviour { // Use this for initialization public Text tt01; //语音识别结果文本 public Text tt02; //吹气数值文本 public static RecognizeManager instance; void Awake(){ instance = this; } void Start () { } // Update is called once per frame void Update () { } public void kaishi() //开始语音识别 { AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"); jo.Call("StartActivity1"); } public void stopp() //停止语音识别 { AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"); jo.Call("stopYuYinShiBie"); } public void kaishiJianCe() //开始监测吹气 { AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"); jo.Call("startJianCe"); } public void tingZhiJianCe(){ //停止监测吹气 AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"); jo.Call("stopJianCe"); } public void xuhaitao(string content) //接收android端的回调 ,语音识别内容 { //接收识别后的语音内容 tt01.text=content; } public void getSoundText(string content){ //接收android端的回调 , 吹气的数量值 tt02.text = content; } }
Android这边脚本:
package com.DFSJ.EDU;
import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import com.DFSJ.EDU.JsonParser;
import com.DFSJ.EDU.MainActivity;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.RecognizerListener;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechRecognizer;
import com.iflytek.cloud.SpeechUtility;
import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends UnityPlayerActivity {
//下面这两个变量是做语音识别的
SpeechRecognizer mIat;
String voiceResult = "";
//下面是获取麦克风音量的
private TextView tView1, tView2;
private Button button1, button2, button3, button4;
private MHandler mHandler;
private MediaRecorder mARecorder;
private File mAudiofile, mSampleDir;
private MicrophoneThread microphone;
// private RecordThread recordThread;
private boolean istrue = true;
private static final String CONSER = "logInfo:";
private Runtime runtime = null;
private Process process = null;// 服务器连接进程
private DataInputStream logInfoStream = null;
public StringBuffer stateInfoParser = null;
Process localProcess;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SpeechUtility.createUtility(this, "appid=579df2b6");
mIat = SpeechRecognizer.createRecognizer(this, mInitListener);
mHandler = new MHandler();
microphone = new MicrophoneThread();
}
//开始语音识别 , unity调用
public void StartActivity1() {
Toast.makeText(this, "kai shi7", Toast.LENGTH_SHORT).show();
mIat.setParameter(SpeechConstant.DOMAIN, "iat");
mIat.setParameter(SpeechConstant.ACCENT, "mandarin");
mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
mIat.setParameter(SpeechConstant.KEY_SPEECH_TIMEOUT, "4000");
mIat.setParameter(SpeechConstant.VAD_EOS, "2000");
mIat.setParameter(SpeechConstant.ASR_PTT, "0");
int ret = mIat.startListening(recognizerListener);
//UnityPlayer.UnitySendMessage("Main Camera", "xuhaitao", "初始化完毕");
if (voiceResult != null || voiceResult != "") {
voiceResult = "";
}
}
//停止语音识别 ,unity调用
public void stopYuYinShiBie() {
mIat.stopListening();
}
private RecognizerListener recognizerListener = new RecognizerListener() {
public void onResult(RecognizerResult results, boolean isLast) {
voiceResult = voiceResult+ JsonParser.parseIatResult(results.getResultString());
if (isLast) {
UnityPlayer.UnitySendMessage("Main Camera", "xuhaitao",voiceResult); //语音识别后,将结果传给unity
}
}
public void onError(SpeechError error) {
// error.getPlainDescription(true);
// UnityPlayer.UnitySendMessage("Main Camera", "onError", "发生错误");
}
// 开始录音
public void onBeginOfSpeech() {
// UnityPlayer.UnitySendMessage("Main Camera", "beginRecord", "开始录音");
}
// 结束录音
public void onEndOfSpeech() {
// UnityPlayer.UnitySendMessage("Main Camera", "finishRecord",
// "onEndOfSpeech");
}
// 扩展用接口
public void onEvent(int eventType, int arg1, int arg2, Bundle obj) {
// UnityPlayer.UnitySendMessage("Main Camera", "xuhaitao", "onEvent");
}
@Override
public void onVolumeChanged(int arg0, byte[] arg1) {
// TODO Auto-generated method stub
// UnityPlayer.UnitySendMessage("Main Camera", "xuhaitao",
// "zhengzai");
}
};
private InitListener mInitListener = new InitListener() {
@Override
public void onInit(int arg0) {
// TODO Auto-generated method stub
// UnityPlayer.UnitySendMessage("Main Camera", "xuhaitao",
// "mInitListener");
if (arg0 == 0) {
// Toast.makeText(MainActivity.this, "login", Toast.LENGTH_SHORT)
// .show();
} else {
// Toast.makeText(MainActivity.this, "login error",
// Toast.LENGTH_SHORT).show();
}
}
};
//下面的内部类和方法都是用于获取麦克风声音的
class MicrophoneThread extends Thread {
/** 获取麦克风的声音 */
final float minAngle = (float) Math.PI * 4 / 11;
float angle;
@Override
public void run() {
while (istrue) {
angle = 100 * minAngle * mARecorder.getMaxAmplitude() / 32768;
if (angle > 100) {
angle = 100;
}
mHandler.sendMessage(mHandler.obtainMessage(1, angle));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class MHandler extends Handler {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch (msg.what) {
case 1:
getSoundText(msg.obj.toString());
break;
}
}
}
//开始监测吹气,unity调用
public void startJianCe() {
// 录音获取麦克风声音
super.onStart();
mARecorder = new MediaRecorder();
mARecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mARecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
mARecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mSampleDir = Environment.getExternalStorageDirectory();
mAudiofile = File.createTempFile("IM" + System.currentTimeMillis(),
".amr", mSampleDir);
} catch (IOException e) {
Log.e("IMMESSAGE", "sdcard access error");
}
mARecorder.setOutputFile(mAudiofile.getAbsolutePath());
try {
mARecorder.prepare();
} catch (Exception e) {
e.printStackTrace();
}
istrue=true;
mARecorder.start();
microphone.start();
}
//停止监测吹气 ,unity调用
public void stopJianCe(){
istrue=false;
mARecorder.stop();
Toast.makeText(this, "kai shi cui qi", Toast.LENGTH_SHORT).show();
}
//将吹气的音量值,回调unity
public void getSoundText(String ss){
UnityPlayer.UnitySendMessage("Main Camera", "getSoundText", ss);
}
}
另外大家要注意的是,语音识别后,需要先关闭语音识别,然后再开始进行吹气音量检测,否则吹气会没反应。同理,吹气任务做完以后,记得要关闭,然后再开始语音识别。