添加了语音识别输入功能

xuejunhao_branch
xuejunhao 3 years ago
parent 40fb20426d
commit 8e5a498a32

@ -31,6 +31,17 @@
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 蓝牙录音使用,不需要可以去除 -->
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<application
android:icon="@drawable/icon_app"

@ -0,0 +1,149 @@
package net.micode.notes.ui;
import java.util.List;
public class ASRresponse {
/**
* results_recognition : ["你好,"]
* result_type : final_result
* best_result :
* origin_result : {"asr_align_begin":80,"asr_align_end":130,"corpus_no":6835867007181645805,"err_no":0,"raf":133,"result":{"word":["你好,"]},"sn":"82d975e0-6eb4-43ac-a0e7-850bb149f28e"}
* error : 0
*/
private String result_type;
private String best_result;
private OriginResultBean origin_result;
private int error;
private List<String> results_recognition;
public String getResult_type() {
return result_type;
}
public void setResult_type(String result_type) {
this.result_type = result_type;
}
public String getBest_result() {
return best_result;
}
public void setBest_result(String best_result) {
this.best_result = best_result;
}
public OriginResultBean getOrigin_result() {
return origin_result;
}
public void setOrigin_result(OriginResultBean origin_result) {
this.origin_result = origin_result;
}
public int getError() {
return error;
}
public void setError(int error) {
this.error = error;
}
public List<String> getResults_recognition() {
return results_recognition;
}
public void setResults_recognition(List<String> results_recognition) {
this.results_recognition = results_recognition;
}
public static class OriginResultBean {
/**
* asr_align_begin : 80
* asr_align_end : 130
* corpus_no : 6835867007181645805
* err_no : 0
* raf : 133
* result : {"word":["你好,"]}
* sn : 82d975e0-6eb4-43ac-a0e7-850bb149f28e
*/
private int asr_align_begin;
private int asr_align_end;
private long corpus_no;
private int err_no;
private int raf;
private ResultBean result;
private String sn;
public int getAsr_align_begin() {
return asr_align_begin;
}
public void setAsr_align_begin(int asr_align_begin) {
this.asr_align_begin = asr_align_begin;
}
public int getAsr_align_end() {
return asr_align_end;
}
public void setAsr_align_end(int asr_align_end) {
this.asr_align_end = asr_align_end;
}
public long getCorpus_no() {
return corpus_no;
}
public void setCorpus_no(long corpus_no) {
this.corpus_no = corpus_no;
}
public int getErr_no() {
return err_no;
}
public void setErr_no(int err_no) {
this.err_no = err_no;
}
public int getRaf() {
return raf;
}
public void setRaf(int raf) {
this.raf = raf;
}
public ResultBean getResult() {
return result;
}
public void setResult(ResultBean result) {
this.result = result;
}
public String getSn() {
return sn;
}
public void setSn(String sn) {
this.sn = sn;
}
public static class ResultBean {
private List<String> word;
public List<String> getWord() {
return word;
}
public void setWord(List<String> word) {
this.word = word;
}
}
}
}

@ -73,6 +73,11 @@ import androidx.core.content.ContextCompat;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.baidu.speech.EventListener;
import com.baidu.speech.EventManager;
import com.baidu.speech.EventManagerFactory;
import com.baidu.speech.asr.SpeechConstant;
import net.micode.notes.R;
import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.TextNote;
@ -86,16 +91,21 @@ import net.micode.notes.ui.NoteEditText.OnTextViewChangeListener;
import net.micode.notes.widget.NoteWidgetProvider_2x;
import net.micode.notes.widget.NoteWidgetProvider_4x;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class NoteEditActivity extends Activity implements OnClickListener,
NoteSettingChangedListener, OnTextViewChangeListener {
NoteSettingChangedListener, OnTextViewChangeListener, EventListener {
private class HeadViewHolder {
public TextView tvModified;
@ -107,6 +117,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
}
private static final Map<Integer, Integer> sBgSelectorBtnsMap = new HashMap<Integer, Integer>();
static {
sBgSelectorBtnsMap.put(R.id.iv_bg_yellow, ResourceParser.YELLOW);
sBgSelectorBtnsMap.put(R.id.iv_bg_red, ResourceParser.RED);
@ -116,6 +127,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
}
private static final Map<Integer, Integer> sBgSelectorSelectionMap = new HashMap<Integer, Integer>();
static {
sBgSelectorSelectionMap.put(ResourceParser.YELLOW, R.id.iv_bg_yellow_select);
sBgSelectorSelectionMap.put(ResourceParser.RED, R.id.iv_bg_red_select);
@ -125,6 +137,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
}
private static final Map<Integer, Integer> sFontSizeBtnsMap = new HashMap<Integer, Integer>();
static {
sFontSizeBtnsMap.put(R.id.ll_font_large, ResourceParser.TEXT_LARGE);
sFontSizeBtnsMap.put(R.id.ll_font_small, ResourceParser.TEXT_SMALL);
@ -133,6 +146,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
}
private static final Map<Integer, Integer> sFontSelectorSelectionMap = new HashMap<Integer, Integer>();
static {
sFontSelectorSelectionMap.put(ResourceParser.TEXT_LARGE, R.id.iv_large_select);
sFontSelectorSelectionMap.put(ResourceParser.TEXT_SMALL, R.id.iv_small_select);
@ -176,6 +190,11 @@ public class NoteEditActivity extends Activity implements OnClickListener,
private final int PHOTO_REQUEST = 1;//请求码
protected EditText txtResult;//识别结果
protected Button startBtn;//开始识别 一直不说话会自动停止,需要再次打开
private EventManager asr;//语音识别核心库
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -187,7 +206,33 @@ public class NoteEditActivity extends Activity implements OnClickListener,
return;
}
initResources();
initView();
txtResult = findViewById(R.id.note_edit_view);
asr = EventManagerFactory.create(this, "asr");
//注册自己的输出事件类
asr.registerListener(this); // EventListener 中 onEvent方法
startBtn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
start();
/*mStartSpeechButton.setBackgroundResource(
R.drawable.bdspeech_btn_orangelight_pressed);*/
break;
case MotionEvent.ACTION_UP:
stop();
/*mStartSpeechButton.setBackgroundResource(
R.drawable.bdspeech_btn_orangelight_normal);*/
break;
default:
return false;
}
return true;
}
});
//根据id获取添加图片按钮
@ -198,8 +243,8 @@ public class NoteEditActivity extends Activity implements OnClickListener,
public void onClick(View view) {
if (ContextCompat.checkSelfPermission(NoteEditActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
//没有授权进行权限申请
ActivityCompat.requestPermissions(NoteEditActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE}, GET_STORAGE_PERMISSION);}
else {
ActivityCompat.requestPermissions(NoteEditActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, GET_STORAGE_PERMISSION);
} else {
Log.d(TAG, "onClick: click add image button");
//ACTION_GET_CONTENT: 允许用户选择特殊种类的数据,并返回(特殊种类的数据:照一张相片或录一段音)
Intent loadImage = new Intent(Intent.ACTION_GET_CONTENT);
@ -374,7 +419,8 @@ public class NoteEditActivity extends Activity implements OnClickListener,
} else {
mNoteHeaderHolder.tvAlertDate.setVisibility(View.GONE);
mNoteHeaderHolder.ivAlertIcon.setVisibility(View.GONE);
};
}
;
}
//路径字符串格式 转换为 图片image格式
@ -491,7 +537,8 @@ public class NoteEditActivity extends Activity implements OnClickListener,
for (int id : sFontSizeBtnsMap.keySet()) {
View view = findViewById(id);
view.setOnClickListener(this);
};
}
;
mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
mFontSizeId = mSharedPrefs.getInt(PREFERENCE_FONT_SIZE, ResourceParser.BG_DEFAULT_FONT_SIZE);
/**
@ -1057,8 +1104,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else
if (isMediaDocument(uri)) {
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
@ -1121,4 +1167,55 @@ public class NoteEditActivity extends Activity implements OnClickListener,
public boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority());
}
private void start() {
Toast.makeText(NoteEditActivity.this, "请开始说话", Toast.LENGTH_SHORT).show();
Map<String, Object> params = new LinkedHashMap<>();//传递Map<String,Object>的参数会将Map自动序列化为json
String event = null;
event = SpeechConstant.ASR_START;
params.put(SpeechConstant.ACCEPT_AUDIO_VOLUME, false);//回调当前音量
String json = null;
json = new JSONObject(params).toString();//demo用json数据来做数据交换的方式
asr.send(event, json, null, 0, 0);// 初始化EventManager对象,这个实例只能创建一次就是我们上方创建的asr此处开始传入
}
private void stop() {
//txtResult.append("停止识别ASR_STOP");
asr.send(SpeechConstant.ASR_STOP, null, null, 0, 0);//此处停止
}
@Override
protected void onDestroy() {
super.onDestroy();
asr.send(SpeechConstant.ASR_CANCEL, "{}", null, 0, 0);
asr.unregisterListener(this);//退出事件管理器
// 必须与registerListener成对出现否则可能造成内存泄露
}
public void onEvent(String name, String params, byte[] data, int offset, int length) {
String resultTxt = null;
//Log.i(TAG, params);
if (name.equals(SpeechConstant.CALLBACK_EVENT_ASR_PARTIAL)) {//识别结果参数
if (params.contains("\"final_result\"")) {//语义结果值
try {
JSONObject json = new JSONObject(params);
String result = json.getString("best_result");//取得key的识别结果
resultTxt = result;
} catch (JSONException e) {
e.printStackTrace();
}
}
}
if (resultTxt != null) {
resultTxt += "\n";
txtResult.append(resultTxt);
}
}
private void initView() {
txtResult = findViewById(R.id.note_edit_view);
startBtn = findViewById(R.id.listen);
//stopBtn = findViewById(R.id.stop);
}
}

@ -109,6 +109,12 @@
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/listen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按住说话" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="7dip"

Loading…
Cancel
Save