陈寒丫丫 2022-11-17 22:29 采纳率: 45.5%
浏览 2
已结题

Rikka音乐播放器搜索页加入一个百度语音识别功能

Rikka音乐播放器想加入一个百度语音识别功能,求一个借鉴方案或源码。

大致逻辑是:在搜索页加入一个按钮,该按钮跳转到语音识别页面,开始识别,将识别出的文字直接调用搜索功能。(感觉有点难)或者多一个步骤,将识别的文字传递到搜索页面,setText(识别result),然后我点击本页面的搜索按钮。

搜索页,目前做的效果,点击右上角的语音ImageVie跳转到百度语音页面

img

百度语音识别页面

img

百度识别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);
    }

}
  • 写回答

3条回答 默认 最新

  • itbird01 Android开发领域新星创作者 2022-11-19 10:51
    关注

    为什么不直接把百度这个activity,用一个intentservice去实现,这样的话,其实不需要中间界面的,正常发起识别,然后searchactivity页面弹一个正在识别的loading,等待结果,然后直接搜索就可以呀

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 11月25日
  • 赞助了问题酬金15元 11月18日
  • 创建了问题 11月17日

悬赏问题

  • ¥15 距离软磁铁一定距离的磁感应强度大小怎么求
  • ¥15 霍尔传感器hmc5883l的xyz轴输出和该点的磁感应强度大小的关系是什么
  • ¥15 vscode开发micropython,import模块出现异常
  • ¥20 Excel数据自动录入表单并提交
  • ¥30 silcavo仿真,30分钟,只需要代码
  • ¥15 FastReport 怎么实现打印后马上关闭打印预览窗口
  • ¥15 利用3支股票数据估计其均值和方差的95%置信区间。
  • ¥15 微信小程序运行一项功能时,弹出未知错误弹框,检查代码没有问题
  • ¥15 ATAC测序生成self-pseudo replicates之前是否要进行去线粒体reads
  • ¥15 python模糊字匹配函数问题