不同设备上的Android语音识别器

时间:2018-10-03 21:41:55

标签: android speech-recognition audio-recording speech-to-text

在测试了多种语音转换为文本的方法之后,我最终得出结论,问题出在我的特定设备上,该代码在一个Android 5设备上运行正常,而在其他运行Android 6的设备上却继续收到错误消息

第二个设备是使用自定义sdk的中文设备,我想知道是否有Android本身需要的特定服务才能启用语音识别器,因为我确实安装了Google语音应用并确实使用adb和同一个问题不断出现

有没有一种方法或调试策略可以用来查找此问题的根源。

public class STT extends AppCompatActivity implements RecognitionListener {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

//android view initialization removed

    String[] PERMISSIONS = {Manifest.permission.RECORD_AUDIO};
    if(!Function.hasPermissions(this, PERMISSIONS)){
        ActivityCompat.requestPermissions(this, PERMISSIONS, REQUEST_PERMISSION_KEY);
    }


    progressBar.setVisibility(View.INVISIBLE);
    speech = SpeechRecognizer.createSpeechRecognizer(this);
    speech.setRecognitionListener(this);
    recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE,
            "en");
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
            this.getPackageName());
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);

    /*
    Minimum time to listen in millis. Here 5 seconds
     */
    recognizerIntent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 5000);
    recognizerIntent.putExtra("android.speech.extra.DICTATION_MODE", true);




    recordbtn.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View p1)
        {
            progressBar.setVisibility(View.VISIBLE);
            speech.startListening(recognizerIntent);
            recordbtn.setEnabled(false);

            /*To stop listening
                progressBar.setVisibility(View.INVISIBLE);
                speech.stopListening();
                recordbtn.setEnabled(true);
             */
        }


    });



}

@Override
public void onResume() {
    super.onResume();
}

@Override
protected void onPause() {
    super.onPause();
    if (speech != null) {
        speech.destroy();
        Log.d("Log", "destroy");
    }

}

@Override
public void onBeginningOfSpeech() {
    Log.d("Log", "onBeginningOfSpeech");
    progressBar.setVisibility(View.VISIBLE);
}

@Override
public void onBufferReceived(byte[] buffer) {
    Log.d("Log", "onBufferReceived: " + buffer);
}

@Override
public void onEndOfSpeech() {
    Log.d("Log", "onEndOfSpeech");
    progressBar.setVisibility(View.INVISIBLE);
    recordbtn.setEnabled(true);
}

@Override
public void onError(int errorCode) {
    String errorMessage = getErrorText(errorCode);
    Log.d("Log", "FAILED " + errorMessage);
    progressBar.setVisibility(View.INVISIBLE);
    returnedText.setText(errorMessage);
    recordbtn.setEnabled(true);
}

@Override
public void onEvent(int arg0, Bundle arg1) {
    Log.d("Log", "onEvent");
}

 @Override
public void onPartialResults(Bundle arg0) {
    Log.d("Log", "onPartialResults");

    ArrayList<String> matches = arg0.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
    String text = "";
    /* To get all close matchs
    for (String result : matches)
    {
        text += result + "\n";
    }
    */
    text = matches.get(0); //  Remove this line while uncommenting above    codes


    returnedText.setText(text);
}

@Override
public void onReadyForSpeech(Bundle arg0) {
    Log.d("Log", "onReadyForSpeech");
}

@Override
public void onResults(Bundle results) {
    Log.d("Log", "onResults");

}

@Override
public void onRmsChanged(float rmsdB) {
    Log.d("Log", "onRmsChanged: " + rmsdB);
    progressBar.setProgress((int) rmsdB);

}


public static String getErrorText(int errorCode) {
    String message;
    switch (errorCode) {
        case SpeechRecognizer.ERROR_AUDIO:
            message = "Audio recording error";
            break;
        case SpeechRecognizer.ERROR_CLIENT:
            message = "Client side error";
            break;
        case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
            message = "Insufficient permissions";
            break;
        case SpeechRecognizer.ERROR_NETWORK:
            message = "Network error";
            break;
        case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
            message = "Network timeout";
            break;
        case SpeechRecognizer.ERROR_NO_MATCH:
            message = "No match";
            break;
        case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
            message = "RecognitionService busy";
            break;
        case SpeechRecognizer.ERROR_SERVER:
            message = "error from server";
            break;
        case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
            message = "No speech input";
            break;
        default:
            message = "Didn't understand, please try again.";
            break;
    }
    return message;
}
}
class Function {

public static  boolean hasPermissions(Context context, String... permissions) {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
    }
    return true;
}
}

1 个答案:

答案 0 :(得分:0)

是的,对“ Google”应用(其ID为com.google.android.googlequicksearchbox)存在依赖性:
您可以检查设备是否已安装,如果没有,请从Google的Play商店中安装。

  

...
  有没有一种方法或调试策略可以用来查找此问题的根源?

LogCat输出中是否没有错误消息?