通过writeaudio方法将录音识别成文字(使用讯飞的SDK)
2015-05-07 20:34
716 查看
因为这个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<byte[]> buffers = splitBuffer(data, data.length, 1280);
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<byte[]> splitBuffer(byte[] buffer, int length, int spsize) {
ArrayList<byte[]> array = new ArrayList<byte[]>();
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<byte[]> buffers) {
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);
}
}
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<byte[]> buffers = splitBuffer(data, data.length, 1280);
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<byte[]> splitBuffer(byte[] buffer, int length, int spsize) {
ArrayList<byte[]> array = new ArrayList<byte[]>();
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<byte[]> buffers) {
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);
}
}
相关文章推荐
- 使用XILINX SDK工具通过网络进行基于linux系统的远程调试方法
- CAJViewer文字识别功能的使用方法
- 百度语音识别SDK使用方法
- 百度语音识别开放平台SDK使用方法
- 百度语音识别REST API使用方法(含JAVA代码)——不需要集成SDK的方法
- 百度语音识别REST API使用方法(含C++代码)——不需要集成SDK的方法
- 百度语音识别开放平台SDK使用方法
- 百度语音识别开放平台SDK使用方法
- 百度语音识别开放平台SDK使用方法
- 图片文字识别软件的作用及使用方法
- 【Java】百度语音识别REST API使用方法不需要集成SDK的方法
- 讯飞语音识别使用writeAudio方法识别失败的问题
- android sdk sdkmanger无界面使用方法(命令行更新SDK)
- python识别图片中的文字处理方法
- eclipse 中main()函数中的String[] args如何使用?通过String[] args验证账号密码的登录类?静态的主方法怎样才能调用非static的方法——通过生成对象?在类中制作一个方法——能够修改对象的属性值?
- Spring中c3p0连接池的配置 及JdbcTemplate的使用 通过XML配置文件注入各种需要对象的操作 来完成数据库添加Add()方法
- python3.6通过urllib模块使用post/get方法
- Mac电脑使用:通过Mac电脑导出iPhone手机里面的视频(不是iTunes导出视频的方法)
- 在Mac上 python中使用tesseract OCR (Pytesser) 识别图片中的文字
- WebView的使用之Android与JS通过WebView互调方法