Android离线语音识别(SpeechRecognizer、PocketSphinx)

最近有开发语音识别功能的需求,但是因为各种原因(主要是收费)不能用国内大厂(百度,讯飞)已有的sdk,于是在网上搜了很多android离线的语音识别,主要有两种:

1.android自带的SpeechRecognizer,但是试了好几种手机,都是提示没有语音识别服务,最终无果,没办法解决,部分代码如下:

 private SpeechRecognizer mSpeechRecognizer;
 mSpeechRecognizer=SpeechRecognizer.createSpeechRecognizer(this);
mSpeechRecognizer.setRecognitionListener(this);

 public void doSpeechRecognition(Context context){
        if(isRecognitionAvailable(context)){
            Intent recognitionIntent=new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            recognitionIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS,true);
            recognitionIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,"zh-CN");
            this.mSpeechRecognizer.startListening(recognitionIntent);
        }else{
            Toast.makeText(context,"没有语音识别服务",Toast.LENGTH_SHORT).show();
        }

    }

 @Override
    public void onReadyForSpeech(Bundle params) {
        Log.i("SpeechRecognition","onReadyForSpeech");
    }

    @Override
    public void onBeginningOfSpeech() {
        Log.i("SpeechRecognition","onBeginningOfSpeech");
    }

    @Override
    public void onRmsChanged(float rmsdB) {
        Log.i("SpeechRecognition","onRmsChanged");
    }

    @Override
    public void onBufferReceived(byte[] buffer) {
        Log.i("SpeechRecognition","onBufferReceived");
    }

    @Override
    public void onEndOfSpeech() {
        Log.i("SpeechRecognition","onEndOfSpeech");
    }

    @Override
    public void onError(int error) {
        Log.i("SpeechRecognition","onError");
    }

    @Override
    public void onResults(Bundle results) {
        ArrayList partialResults=results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
        if(partialResults!=null && partialResults.size()>0) {
            String bestResult = partialResults.get(0);
            Log.i("SpeechRecognition","onResults bestResult="+bestResult);
            //result.setText(bestResult);
        }
    }

2.PocketSphinx,不多说,目前感觉是唯一的救命稻草,基本能用,单个短语容易误识别

参考文章:https://www.cnblogs.com/elecdog/p/7235235.html

https://www.jianshu.com/p/82ffb90cc463

参考demo如下:https://download.csdn.net/download/android_xc/12274142

你可能感兴趣的:(Android离线语音识别(SpeechRecognizer、PocketSphinx))