Android studio集成百度语音识别api

          今天 项目中要用到语音功能,所以看了下百度语音api

   1,根据百度语音开发文档,创建应用,下载相应的jar包,添加到你的应用

    2,清单文件配置信息,

        

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

       

<meta-data
    android:name="com.baidu.speech.APP_ID"
    android:value="xxxxxxx" />
<!-- 请填写应用实际的API_KEY -->
<meta-data
    android:name="com.baidu.speech.API_KEY"
    android:value="xxxxxxxxxx" />
<!-- 请填写应用实际的SECRET_KEY -->
<meta-data
    android:name="com.baidu.speech.SECRET_KEY"
    android:value="xxxxxxxxx" />

3 ,把drawable和raw下的资源全部复制到你的项目,注意:如果缺少资源会报错

4 ,把so文件复制到jnilibs目录,这点很重要,我也是看下面这我博主文章才觉得这个问题

  https://my.oschina.net/MStart/blog/637723?p={{page}} 

4,按照官方api代码编写,根据自己的需求写布局,贴上demo代码

public class MainActivity extends Activity implements RecognitionListener {
    private static final String TAG ="MainActivity" ;
    private SpeechRecognizer speechRecognizer;
    private BaiduASRDigitalDialog dialog;

    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 Button setting,start;
    private TextView result,txtLog;
    private long time;
    private long speechEndTime=-1;
    private static final int REQUEST_UI=1;
    private int status = STATUS_None;
    private static final int EVENT_ERROR = 11;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setting= (Button) findViewById(R.id.setting);
        start= (Button) findViewById(R.id.start);
        result= (TextView) findViewById(R.id.result);
        txtLog= (TextView) findViewById(R.id.txtLog);
        // 创建识别器
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
            speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this, new ComponentName(this, VoiceRecognitionService.class));
        }
        // 注册监听器
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
            speechRecognizer.setRecognitionListener(this);
        }

    }
    //设置界面
    public void setTing(View view){
        Intent intent = new Intent("com.baidu.speech.asr.demo.setting");
        startActivity(intent);
    }
    //开始
  public void start(View view){
      SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
      boolean api = sp.getBoolean("api", false);
      if (api) {
      
         
          switch (status) {
              case STATUS_None:
                  startASR();
                  start.setText("取消");
                  status = STATUS_WaitingReady;
                  break;
              case STATUS_WaitingReady:
                  cancel();
                  status = STATUS_None;
                  start.setText("开始");
                  break;
              case STATUS_Ready:
                  cancel();
                  status = STATUS_None;
                  start.setText("开始");
                  break;
              case STATUS_Speaking:
                  stop();
                  status = STATUS_Recognition;
                  start.setText("识别中");
                  break;
              case STATUS_Recognition:
                  cancel();
                  status = STATUS_None;
                  start.setText("开始");
                  break;
          }
      } else {
           startASR();
      }
  }

    private void stop() {
        speechRecognizer.stopListening();
        print("点击了“说完了”");
    }

    private void cancel() {
        speechRecognizer.cancel();
        status = STATUS_None;
        print("点击了“取消”");
    }
    // 开始识别
    void startASR() {
       // txtLog.setText("");
        print("点击了“开始”");
        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;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
                speechRecognizer.startListening(intent);
            }
        } else {
           intent.setAction("com.baidu.action.RECOGNIZE_SPEECH");
            startActivityForResult(intent, REQUEST_UI);

        }

       // result.setText("");



    }
    void bindParams(Intent intent) {
        // 设置识别参数
 //       intent.putExtra("sample", 16000); // 离线仅支持16000采样率
 //      intent.putExtra("language", "cmn-Hans-CN"); // 离线仅支持中文普通话
//        intent.putExtra("prop", 20000); // 输入
//        intent.putExtra("prop", 10060); // 地图
//        intent.putExtra("prop", 10001); // 音乐
//        intent.putExtra("prop", 10003); // 应用
//        intent.putExtra("prop", 10008); // 电话
//        intent.putExtra("prop", 100014); // 联系人
//        intent.putExtra("prop", 100016); // 手机设置
//        intent.putExtra("prop", 100018); // 电视指令
//        intent.putExtra("prop", 100019); // 播放器指令
//        intent.putExtra("prop", 100020); // 收音机指令
//        intent.putExtra("prop", 100021); // 命令词


        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_SUCCESS, R.raw.bdspeech_recognition_success);
            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.getBoolean(Constant.EXTRA_GRAMMAR, false)) {
            intent.putExtra(Constant.EXTRA_GRAMMAR, "assets:///baidu_speech_grammar.bsg");
        }
        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_NLU)) {
            String tmp = sp.getString(Constant.EXTRA_NLU, "").replaceAll(",.*", "").trim();
            if (null != tmp && !"".equals(tmp)) {
                intent.putExtra(Constant.EXTRA_NLU, 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");
            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();
    }

    @Override
    public void onReadyForSpeech(Bundle bundle) {
        // 准备就绪
        print("准备就绪,可以开始说话");

    }

    @Override
    public void onBeginningOfSpeech() {
        // 开始说话处理
        time = System.currentTimeMillis();
        status = STATUS_Speaking;
        start.setText("说完了");
        print("检测到用户的已经开始说话");



    }

    @Override
    public void onRmsChanged(float v) {
        // 音量变化处理

    }

    @Override
    public void onBufferReceived(byte[] bytes) {
        // 录音数据传出处理

    }

    @Override
    public void onEndOfSpeech() {
        // 说话结束处理
        speechEndTime = System.currentTimeMillis();
        status = STATUS_Recognition;
        print("检测到用户的已经停止说话");
        start.setText("识别中");
    }

    @Override
    public void onError(int i) {
        // 出错处理
        time = 0;
        StringBuilder sb = new StringBuilder();
        switch (i) {
            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(":" + i);
        print("识别失败:" + sb.toString());
        start.setText("开始");

    }

    @Override
    public void onResults(Bundle bundle) {
        // 最终结果处理
        long end2finish = System.currentTimeMillis() - speechEndTime;
        status = STATUS_None;
        ArrayList<String> nbest = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
        print("识别成功:" + Arrays.toString(nbest.toArray(new String[nbest.size()])));
        String json_res = bundle.getString("origin_result");
        try {
            print("origin_result=\n" + new JSONObject(json_res).toString(4));
        } catch (Exception e) {
            print("origin_result=[warning: bad json]\n" + json_res);
        }
        start.setText("开始");
        String strEnd2Finish = "";
        if (end2finish < 60 * 1000) {
            strEnd2Finish = "(waited " + end2finish + "ms)";
        }
        result.setText(nbest.get(0) + strEnd2Finish);
        time = 0;

    }

    @Override
    public void onPartialResults(Bundle bundle) {
        // 临时结果处理
        ArrayList<String> nbest = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
        if (nbest.size() > 0) {
            print("~临时识别结果:" + Arrays.toString(nbest.toArray(new String[0])));
            result.setText(nbest.get(0));
        }
    }

    @Override
    public void onEvent(int i, Bundle bundle) {
        // 处理事件回调

        switch (i) {
            case EVENT_ERROR:
                String reason = bundle.get("reason") + "";
                print("EVENT_ERROR, " + reason);
                break;
            case VoiceRecognitionService.EVENT_ENGINE_SWITCH:
                int type = bundle.getInt("engine_type");
                print("*引擎切换至" + (type == 0 ? "在线" : "离线"));
                break;
        }
    }

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

    private void print(String msg) {
      
        long t = System.currentTimeMillis() - time;
        if (t > 0 && t < 100000) {
            txtLog.append(t + "ms, " + msg + "\n");
        } else {
            txtLog.append("" + msg + "\n");
        }
        ScrollView sv = (ScrollView) txtLog.getParent();
        sv.smoothScrollTo(0, 1000000);
        Log.d(TAG, "----" + msg);
    }
}

            因为官方提供的是eclipse 环境demo,所以只要注意 3 ,4两点,就很简单了

猜你喜欢

转载自blog.csdn.net/x995630736/article/details/72918631