因为这个demo用到了讯飞语音,所以需要申请讯飞的key值,另外对录音的格式也有一定的要求:16bit,16000hz,单声道的pcm文件,因为布局只有一个button和两个Textview,其中两个textview显示的结果不一样,用不到的同学可自行去掉,另外还需要添加权限,导入jar包,所以就不上传了。下面就是代码:
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
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 android.os.Bundle;
import android.os.Environment;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
@SuppressLint("NewApi")
public class MainActivity extends Activity implements OnClickListener {
private SpeechRecognizer mSpeech;
private TextView recognizertext, allreult;
private ProgressDialog progressDialog;
private StringBuffer ret1, ret2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.recognizerB).setOnClickListener(this);
recognizertext = (TextView) findViewById(R.id.recognizertext);
allreult = (TextView) findViewById(R.id.allreult);
SpeechUtility.createUtility(MainActivity.this, "appid=12345678");
mSpeech = SpeechRecognizer.createRecognizer(this, mInitListener);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.recognizerB:
progressDialog = ProgressDialog.show(MainActivity.this, null,
"正在识别...", true);
recognizertext.setText(null);
allreult.setText(null);
findViewById(R.id.recognizerB).setEnabled(false);
String file_path = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/reverseme.pcm";
byte[] data = readFileFromSDcard(MainActivity.this, file_path);
ArrayList
writeaudio(buffers);
break;
default:
break;
}
}
private byte[] readFileFromSDcard(Context context, String filepath) {
byte[] buffer = null;
FileInputStream in = null;
try {
in = new FileInputStream(filepath);
buffer = new byte[in.available()];
in.read(buffer);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
in = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
return buffer;
}
public ArrayList
ArrayList
if (spsize <= 0 || length <= 0 || buffer == null
|| buffer.length < length)
return array;
int size = 0;
while (size < length) {
int left = length - size;
if (spsize < left) {
byte[] sdata = new byte[spsize];
System.arraycopy(buffer, size, sdata, 0, spsize);
array.add(sdata);
size += spsize;
} else {
byte[] sdata = new byte[left];
System.arraycopy(buffer, size, sdata, 0, left);
array.add(sdata);
size += left;
}
}
return array;
}
public void writeaudio(final ArrayList
new Thread(new Runnable() {
@Override
public void run() {
mSpeech.setParameter(SpeechConstant.DOMAIN, "iat");
mSpeech.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
mSpeech.setParameter(SpeechConstant.AUDIO_SOURCE, "-1");
// 设置多个候选结果
mSpeech.setParameter(SpeechConstant.ASR_NBEST, "3");
mSpeech.setParameter(SpeechConstant.ASR_WBEST, "3");
mSpeech.startListening(mRecognizerListener);
for (int i = 0; i < buffers.size(); i++) {
try {
mSpeech.writeAudio(buffers.get(i), 0,
buffers.get(i).length);
Thread.sleep(40);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
mSpeech.stopListening();
}
}).start();
}
/**
* 初始化监听器。
*/
private InitListener mInitListener = new InitListener() {
@Override
public void onInit(int code) {
Log.d("mytest", "SpeechRecognizer init() code = " + code);
}
};
public RecognizerListener mRecognizerListener = new RecognizerListener() {
@Override
public void onBeginOfSpeech() {
// TODO Auto-generated method stub
}
@Override
public void onEndOfSpeech() {
// TODO Auto-generated method stub
}
@Override
public void onEvent(int arg0, int arg1, int arg2, String arg3) {
// TODO Auto-generated method stub
}
@Override
public void onVolumeChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onError(SpeechError arg0) {
// TODO Auto-generated method stub
findViewById(R.id.recognizerB).setEnabled(true);
}
@Override
public void onResult(RecognizerResult results, boolean arg1) {
// 正确的结果
String text = parseIatResult(results.getResultString());
recognizertext.append(text);
// 不标准结果
allreult.append(ret2.toString());
if (arg1) {
progressDialog.dismiss();
findViewById(R.id.recognizerB).setEnabled(true);
String text1, text2;
text1 = recognizertext.getText().toString();
text2 = allreult.getText().toString();
boolean isequls = text1.regionMatches(true, 0, text2, 0, 0);
Log.i("joResult", "isequls=" + isequls);
}
}
};
private String parseIatResult(String json) {
ret1 = new StringBuffer();
ret2 = new StringBuffer();
try {
JSONTokener tokener = new JSONTokener(json);
JSONObject joResult = new JSONObject(tokener);
Log.i("joResult", "joResult:" + joResult.toString());
JSONArray words = joResult.getJSONArray("ws");
for (int i = 0; i < words.length(); i++) {
// 转写结果词,默认使用第一个结果
JSONArray items = words.getJSONObject(i).getJSONArray("cw");
JSONObject obj = items.getJSONObject(0);
ret1.append(obj.getString("w"));
Log.i("joResult", "items.length():" + items.length());
switch (items.length()) {
case 1:
JSONObject obj1 = items.getJSONObject(0);
ret2.append(obj1.getString("w"));
break;
case 2:
JSONObject obj2 = items.getJSONObject(1);
ret2.append(obj2.getString("w"));
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return ret1.toString();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
mSpeech.stopListening();
progressDialog.dismiss();
return true;
}
return super.onKeyDown(keyCode, event);
}
}