Rikka音乐播放器想加入一个百度语音识别功能,求一个借鉴方案或源码。
大致逻辑是:在搜索页加入一个按钮,该按钮跳转到语音识别页面,开始识别,将识别出的文字直接调用搜索功能。(感觉有点难)或者多一个步骤,将识别的文字传递到搜索页面,setText(识别result),然后我点击本页面的搜索按钮。
搜索页,目前做的效果,点击右上角的语音ImageVie跳转到百度语音页面
百度语音识别页面
百度识别API功能activity部分代码,我想要将asRresponse.getBest_result()识别出来的文字直接通过SearchActivity搜索功能搜索歌名或歌曲。
public class SpeechRecognitionActivity extends AppCompatActivity implements EventListener {
protected SearchEditText etSearch;
protected Button startBtn; // 开始
protected Button stopBtn; // 停止
private EventManager asr;//语音识别核心库
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recording);
# 添加一张gif图片
ImageView asr_gif = (ImageView) findViewById(R.id.img_gif);
Glide.with(this).load(R.drawable.asr).into(asr_gif);
initPermission();
initView();
// 初始化EventEventManager对象
asr = EventManagerFactory.create(this, "asr");
// 注册自己的输出事件类
asr.registerListener(this);
}
……
@Override
public void onEvent(String name, String params, byte[] data, int offset, int length) {
if (name.equals(SpeechConstant.CALLBACK_EVENT_ASR_PARTIAL)) {
// 识别相关的结果都在这里
if (params == null || params.isEmpty()) {
return;
}
if (params.contains("\"final_result\"")) {
// 一句话的最终识别结果
Log.i("asr.event", params);
Gson gson = new Gson();
AsrResponseBean asRresponse = gson.fromJson(params, AsrResponseBean.class);//数据解析转实体bean
if(asRresponse == null) return;
//从日志中,得出Best_result的值才是需要的,但是后面跟了一个中文输入法下的逗号,
if(asRresponse.getBest_result().contains(",")){//包含逗号 则将逗号替换为空格
etSearch.setText(asRresponse.getBest_result().replace(',',' ').trim());//替换为空格之后,通过trim去掉字符串的首尾空格
}else {//不包含
etSearch.setText(asRresponse.getBest_result().trim());
}
}
}
}
}
SearchActivity.java
public class SearchActivity extends BaseActivity<SearchPresenter> implements SearchContract.View {
private static final String TAG = "SearchActivity";
public static final String KEYWORDS = "keywords";
@BindView(R.id.tl_searchhistory)
SearchHistoryTagLayout tlHistory;
@BindView(R.id.rv_hotsearch)
RecyclerView rvHotSearch;
@BindView(R.id.et_search) // 搜索框
SearchEditText etSearch;
@BindView((R.id.iv_speech)) // 语音识别
ImageView iv_speech;
private HotSearchAdapter adapter;
private List<SearchHistoryBean> stringList = new ArrayList<>();
private HotSearchDetailBean searchDetailBean;
private RikkaMusicDialog isDeleteDialog;
@Override
protected void onCreateView(Bundle savedInstanceState) {
setContentView(R.layout.activity_search);
ImmersionBar.with(this)
.transparentBar()
.statusBarColor(R.color.colorPrimary)
.statusBarDarkFont(false)
.init();
}
@Override
protected SearchPresenter onCreatePresenter() {
return new SearchPresenter(this);
}
@Override
protected void initModule() {
ButterKnife.bind(this);
}
@Override
protected void initData() {
setBackBtn(getString(R.string.colorWhite));
setEditText(getString(R.string.colorTransWithe));
setRightSearchButton();
adapter = new HotSearchAdapter(this);
LinearLayoutManager manager = new LinearLayoutManager(this);
rvHotSearch.setLayoutManager(manager);
rvHotSearch.setAdapter(adapter);
adapter.setListener(searchListener);
showDialog();
mPresenter.getHotSearchDetail();
}
@Override
protected void onResume() {
super.onResume();
stringList.clear();
//从GreenDao里拿搜索历史
if (SearchHistoryDaoOp.queryAll(this) != null) {
stringList = SearchHistoryDaoOp.queryAll(this);
}
tlHistory.addHistoryText(stringList, tagListener);
}
private SearchHistoryTagLayout.OnHistoryTagClickListener tagListener = position -> {
String keywords = stringList.get(position).getKeyowrds();
searchSong(keywords);
};
private HotSearchAdapter.OnHotSearchAdapterClickListener searchListener = position -> {
if (searchDetailBean != null) {
String keywords = searchDetailBean.getData().get(position).getSearchWord();
searchSong(keywords);
}
};
@Override
@OnClick({R.id.iv_search_name, R.id.iv_rubbish_bin, R.id.iv_speech})
public void onClick(View v) {
if (ClickUtil.isFastClick(1000, v)) {
return;
}
Intent intent = new Intent();
switch (v.getId()) {
// TODO
case R.id.iv_search_name:
String keywords = etSearch.getKeyWords();
if (!TextUtils.isEmpty(keywords)) {
searchSong(keywords);
} else {
ToastUtils.show("请输入关键字!");
}
break;
case R.id.iv_speech:
intent.setClass(SearchActivity.this, SpeechRecognitionActivity.class);
startActivity(intent);
break;
case R.id.iv_rubbish_bin:
showIsDeleteAllDialog();
break;
}
}
private void showIsDeleteAllDialog() {
if (isDeleteDialog == null) {
isDeleteDialog = new RikkaMusicDialog.Builder(this)
.setMsg(R.string.should_delete_all_search_history)
.setNegativeText(R.string.dialog_cancel)
.setPositiveText(R.string.dialog_search_history_clear)
.setNegativeClickListener((dialog, which) -> {
dialog.dismiss();
})
.setPositiveClickListener((dialog, which) -> {
dialog.dismiss();
SearchHistoryDaoOp.deleteAllData(this);
stringList = SearchHistoryDaoOp.queryAll(this);
tlHistory.addHistoryText(stringList, tagListener);
}).create();
}
if (!isDeleteDialog.isShowing()) {
isDeleteDialog.show();
}
}
//根据关键字去搜索
private void searchSong(String keywords) {
stringList.add(new SearchHistoryBean(keywords));
if (stringList.size() > 10) {
stringList.remove(0);
}
for (int i = 0; i < stringList.size() - 1; i++) {
//去重
if (stringList.get(i).getKeyowrds().equals(keywords)) {
stringList.remove(i);
break;
}
}
SearchHistoryDaoOp.saveData(this, stringList);
Intent intent = new Intent(SearchActivity.this, SearchResultActivity.class);
intent.putExtra(KEYWORDS, keywords);
startActivity(intent);
}
@Override
public void onGetHotSearchDetailSuccess(HotSearchDetailBean bean) {
hideDialog();
LogUtil.d(TAG, "onGetHotSearchDetailSuccess : " + bean);
searchDetailBean = bean;
List<HotSearchDetailBean> adapterList = new ArrayList<>();
adapterList.add(searchDetailBean);
adapter.notifyDataSetChanged(adapterList);
}
@Override
public void onGetHotSearchDetailFail(String e) {
hideDialog();
LogUtil.e(TAG, "onGetHotSearchDetailFail : " + e);
ToastUtils.show(e);
}
}