Skip to content

Commit

Permalink
实验四
Browse files Browse the repository at this point in the history
实验四
  • Loading branch information
DUDUfine committed Jun 16, 2016
1 parent c6e6dbe commit c159e5f
Show file tree
Hide file tree
Showing 7 changed files with 546 additions and 40 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,348 @@
package com.baidu.android.voicedemo;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.speech.RecognitionListener;
import android.speech.SpeechRecognizer;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.*;
import com.baidu.speech.VoiceRecognitionService;
import com.baidu.speech.recognizerdemo.R;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;



import java.util.*;

public class ApiActivity extends Activity implements RecognitionListener {
private static final String TAG = "Sdk2Api";
private static final int REQUEST_UI = 1;
private Button btn;
private ListView contentlistView;
private DBHelper dbHelper;
private SQLiteDatabase dbRead,dbWrite;
private SimpleCursorAdapter adapter;

public static final int STATUS_None = 0;
public static final int STATUS_WaitingReady = 2;
public static final int STATUS_Ready = 3;
public static final int STATUS_Speaking = 4;
public static final int STATUS_Recognition = 5;
private SpeechRecognizer speechRecognizer;
private int status = STATUS_None;
private TextView txtResult;
private long speechEndTime = -1;
private static final int EVENT_ERROR = 11;


private AdapterView.OnItemLongClickListener listViewItemLongClickListener=new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,final int position, long id) {
new AlertDialog.Builder(ApiActivity.this).setTitle("提醒").setMessage("您确定要删除该项吗?").setNegativeButton("取消", null).setPositiveButton("确定", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface arg0, int arg1) {
Cursor cursor=adapter.getCursor();
cursor.moveToPosition(position);
int itemId=cursor.getInt(cursor.getColumnIndex("_id"));
dbWrite.delete("Account", "_id=?", new String[]{itemId+""});
refreshListView();

}
}).show();

return true;
}
};


@SuppressWarnings("deprecation")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.sdk2_api);
contentlistView = (ListView) findViewById(R.id.contentListView);
btn = (Button) findViewById(R.id.btn);
dbHelper = new DBHelper(this);
dbWrite = dbHelper.getWritableDatabase();
dbRead = dbHelper.getReadableDatabase();

speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this, new ComponentName(this, VoiceRecognitionService.class));
speechRecognizer.setRecognitionListener(this);


adapter = new SimpleCursorAdapter(this,
R.layout.account_record,
null,
new String[]{"result","comsumedate"},
new int[]{R.id.txtResult,R.id.comsumedate});
contentlistView.setAdapter(adapter);
refreshListView();
btn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(ApiActivity.this);
boolean api = sp.getBoolean("api", false);//默认有录音对话框
start();

}
});

contentlistView.setOnItemLongClickListener(listViewItemLongClickListener);
}

@Override
protected void onDestroy() {
speechRecognizer.destroy();
super.onDestroy();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
onResults(data.getExtras());
}
}

public void bindParams(Intent intent) {
intent.putExtra(Constant.EXTRA_NLU, "enable");
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
if (sp.getBoolean("tips_sound", true)) {
intent.putExtra(Constant.EXTRA_SOUND_START, R.raw.bdspeech_recognition_start);
intent.putExtra(Constant.EXTRA_SOUND_END, R.raw.bdspeech_speech_end);
intent.putExtra(Constant.EXTRA_SOUND_ERROR, R.raw.bdspeech_recognition_error);
intent.putExtra(Constant.EXTRA_SOUND_CANCEL, R.raw.bdspeech_recognition_cancel);

}
if (sp.contains(Constant.EXTRA_INFILE)) {
String tmp = sp.getString(Constant.EXTRA_INFILE, "").replaceAll(",.*", "").trim();
intent.putExtra(Constant.EXTRA_INFILE, tmp);
}
if (sp.getBoolean(Constant.EXTRA_OUTFILE, false)) {
intent.putExtra(Constant.EXTRA_OUTFILE, "sdcard/outfile.pcm");
}
if (sp.contains(Constant.EXTRA_SAMPLE)) {
String tmp = sp.getString(Constant.EXTRA_SAMPLE, "").replaceAll(",.*", "").trim();
if (null != tmp && !"".equals(tmp)) {
intent.putExtra(Constant.EXTRA_SAMPLE, Integer.parseInt(tmp));
}
}
if (sp.contains(Constant.EXTRA_LANGUAGE)) {
String tmp = sp.getString(Constant.EXTRA_LANGUAGE, "").replaceAll(",.*", "").trim();
if (null != tmp && !"".equals(tmp)) {
intent.putExtra(Constant.EXTRA_LANGUAGE, tmp);
}
}

if (sp.contains(Constant.EXTRA_VAD)) {
String tmp = sp.getString(Constant.EXTRA_VAD, "").replaceAll(",.*", "").trim();
if (null != tmp && !"".equals(tmp)) {
intent.putExtra(Constant.EXTRA_VAD, tmp);
}
}
String prop = null;
if (sp.contains(Constant.EXTRA_PROP)) {
String tmp = sp.getString(Constant.EXTRA_PROP, "").replaceAll(",.*", "").trim();
if (null != tmp && !"".equals(tmp)) {
intent.putExtra(Constant.EXTRA_PROP, Integer.parseInt(tmp));
prop = tmp;
}
}

// offline asr
{
intent.putExtra(Constant.EXTRA_OFFLINE_ASR_BASE_FILE_PATH, "/sdcard/easr/s_1");
intent.putExtra(Constant.EXTRA_LICENSE_FILE_PATH, "/sdcard/easr/license-tmp-20150530.txt");
if (null != prop) {
int propInt = Integer.parseInt(prop);
if (propInt == 10060) {
intent.putExtra(Constant.EXTRA_OFFLINE_LM_RES_FILE_PATH, "/sdcard/easr/s_2_Navi");
} else if (propInt == 20000) {
intent.putExtra(Constant.EXTRA_OFFLINE_LM_RES_FILE_PATH, "/sdcard/easr/s_2_InputMethod");
}
}
intent.putExtra(Constant.EXTRA_OFFLINE_SLOT_DATA, buildTestSlotData());
}
}

private String buildTestSlotData() {
JSONObject slotData = new JSONObject();
JSONArray name = new JSONArray().put("李涌泉").put("郭下纶");
JSONArray song = new JSONArray().put("七里香").put("发如雪");
JSONArray artist = new JSONArray().put("周杰伦").put("李世龙");
JSONArray app = new JSONArray().put("手机百度").put("百度地图");
JSONArray usercommand = new JSONArray().put("关灯").put("开门");
try {
slotData.put(Constant.EXTRA_OFFLINE_SLOT_NAME, name);
slotData.put(Constant.EXTRA_OFFLINE_SLOT_SONG, song);
slotData.put(Constant.EXTRA_OFFLINE_SLOT_ARTIST, artist);
slotData.put(Constant.EXTRA_OFFLINE_SLOT_APP, app);
slotData.put(Constant.EXTRA_OFFLINE_SLOT_USERCOMMAND, usercommand);
} catch (JSONException e) {

}
return slotData.toString();
}

private void start() {
Intent intent = new Intent();
bindParams(intent);
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
{

String args = sp.getString("args", "");
if (null != args) {
// print("参数集:" + args);
intent.putExtra("args", args);
}
}
boolean api = sp.getBoolean("api", false);
if (api) {
speechEndTime = -1;
speechRecognizer.startListening(intent);
} else {
intent.setAction("com.baidu.action.RECOGNIZE_SPEECH");
startActivityForResult(intent, REQUEST_UI);
}


}


private void stop() {
speechRecognizer.stopListening();

}

private void cancel() {
speechRecognizer.cancel();
status = STATUS_None;

}

@Override
public void onReadyForSpeech(Bundle params) {
status = STATUS_Ready;

}

@Override
public void onBeginningOfSpeech() {
status = STATUS_Speaking;

}

@Override
public void onRmsChanged(float rmsdB) {

}

@Override
public void onBufferReceived(byte[] buffer) {

}

@Override
public void onEndOfSpeech() {
speechEndTime = System.currentTimeMillis();
status = STATUS_Recognition;

}

@Override
public void onError(int error) {
status = STATUS_None;
StringBuilder sb = new StringBuilder();
switch (error) {
case SpeechRecognizer.ERROR_AUDIO:
sb.append("音频问题");
break;
case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
sb.append("没有语音输入");
break;
case SpeechRecognizer.ERROR_CLIENT:
sb.append("其它客户端错误");
break;
case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
sb.append("权限不足");
break;
case SpeechRecognizer.ERROR_NETWORK:
sb.append("网络问题");
break;
case SpeechRecognizer.ERROR_NO_MATCH:
sb.append("没有匹配的识别结果");
break;
case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
sb.append("引擎忙");
break;
case SpeechRecognizer.ERROR_SERVER:
sb.append("服务端错误");
break;
case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
sb.append("连接超时");
break;
}
sb.append(":" + error);

}

@Override
public void onResults(Bundle results) {
status = STATUS_None;
ArrayList<String> nbest = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
addDataToSql(nbest.get(0));

}

@Override
public void onPartialResults(Bundle partialResults) {
ArrayList<String> nbest = partialResults.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);

}
private void addDataToSql(String txtresult) {
String result=txtresult;
ContentValues cv=new ContentValues();
cv.put("result", result);
dbWrite.insert("Account", null, cv);
refreshListView();

}
private void refreshListView(){
dbRead=dbHelper.getReadableDatabase();
Cursor c=dbRead.query("Account", null, null, null, null, null, "Comsumedate desc");
adapter.changeCursor(c);

}
@Override
public void onEvent(int eventType, Bundle params) {
switch (eventType) {
case EVENT_ERROR:
String reason = params.get("reason") + "";
break;
case VoiceRecognitionService.EVENT_ENGINE_SWITCH:
// int type = params.getInt("engine_type");
// print("*引擎切换至" + (type == 0 ? "在线" : "离线"));
break;
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.baidu.android.voicedemo;

public class Constant {
public static final String EXTRA_KEY = "key";
public static final String EXTRA_SECRET = "secret";
public static final String EXTRA_SAMPLE = "sample";
public static final String EXTRA_SOUND_START = "sound_start";
public static final String EXTRA_SOUND_END = "sound_end";
public static final String EXTRA_SOUND_SUCCESS = "sound_success";
public static final String EXTRA_SOUND_ERROR = "sound_error";
public static final String EXTRA_SOUND_CANCEL = "sound_cancel";
public static final String EXTRA_INFILE = "infile";
public static final String EXTRA_OUTFILE = "outfile";

public static final String EXTRA_LANGUAGE = "language";
public static final String EXTRA_NLU = "nlu";
public static final String EXTRA_VAD = "vad";
public static final String EXTRA_PROP = "prop";

public static final String EXTRA_OFFLINE_ASR_BASE_FILE_PATH = "asr-base-file-path";
public static final String EXTRA_LICENSE_FILE_PATH = "license-file-path";
public static final String EXTRA_OFFLINE_LM_RES_FILE_PATH = "lm-res-file-path";
public static final String EXTRA_OFFLINE_SLOT_DATA = "slot-data";
public static final String EXTRA_OFFLINE_SLOT_NAME = "name";
public static final String EXTRA_OFFLINE_SLOT_SONG = "song";
public static final String EXTRA_OFFLINE_SLOT_ARTIST = "artist";
public static final String EXTRA_OFFLINE_SLOT_APP = "app";
public static final String EXTRA_OFFLINE_SLOT_USERCOMMAND = "usercommand";

public static final int SAMPLE_8K = 8000;
public static final int SAMPLE_16K = 16000;

public static final String VAD_SEARCH = "search";
public static final String VAD_INPUT = "input";
}
Loading

0 comments on commit c159e5f

Please sign in to comment.