我需要一些关于android Speech Recognizer API的帮助

时间:2013-11-30 15:36:00

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

我需要一些使用Android Speech API将语音转换为文本的帮助。 API在我的设备(Android版本2.3.5)上给出了正确的结果,但是当我在具有Android版本4.1.2的设备上进行测试时,它给出了异常结果。就像结果重复多次一样。如果有人遇到这个问题你能告诉我如何解决这个问题吗?

以下是我正在使用的代码:

public class MainActivity extends Activity {

    protected static final int RESULT_SPEECH = 1;
    protected static final String TAG = "MY_TAG";

    private TextView spokenText;
    private Button spkButton;
    private Button stopButton;
    private SpeechRecognizer sR;
    private ClickListener clickListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        clickListener = new ClickListener();
        spokenText = (TextView) findViewById(R.id.spokenText);
        spokenAnswer = (TextView) findViewById(R.id.spokenAnswer);
        spkButton = (Button) findViewById(R.id.speakButton);
        stopButton = (Button) findViewById(R.id.stopButton);

        spkButton.setOnClickListener(clickListener);
        stopButton.setOnClickListener(clickListener);

        sR = SpeechRecognizer.createSpeechRecognizer(this);
        sR.setRecognitionListener(new listener());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void startListening()
    {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,1); 
        intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,this.getPackageName());
        sR.startListening(intent);
    }

    public void stopListening()
    {
        sR.stopListening();
    }

    class ClickListener implements OnClickListener
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(v == spkButton)
            {
                startListening();
            }
            else if(v == stopButton)
            {
                stopListening();
            }

        }

    }
    class listener implements RecognitionListener{

        @Override
        public void onRmsChanged(float rmsdB) {
            // TODO Auto-generated method stub
            //Log.d(TAG, "onRmsChanged");
        }

        @Override
        public void onResults(Bundle results) {
            // TODO Auto-generated method stub
            String str = new String();
            //Log.d(TAG, "onResults " + results);
            ArrayList<String> data = results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
            for (int i = 0; i < data.size(); i++)
            {
                      //Log.d(TAG, "result " + data.get(i));
                      str += data.get(i);
            }
            Log.d(TAG, str);
            spokenText.setText(str); 
        }

        @Override
        public void onReadyForSpeech(Bundle params) {
            // TODO Auto-generated method stub
            //Log.d(TAG, "onReadyForSpeech");
        }

        @Override
        public void onPartialResults(Bundle partialResults) {
            // TODO Auto-generated method stub
            //Log.d(TAG, "onPartialResults");
        }

        @Override
        public void onEvent(int eventType, Bundle params) {
            // TODO Auto-generated method stub
            //Log.d(TAG, "onEvent");
        }

        @Override
        public void onError(int error) {
            // TODO Auto-generated method stub
            String mError = "";
            switch (error) {
            case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:                
                mError = " network timeout"; 
                break;
            case SpeechRecognizer.ERROR_NETWORK: 
                mError = " network" ;
                return;
            case SpeechRecognizer.ERROR_AUDIO: 
                mError = " audio"; 
                break;
            case SpeechRecognizer.ERROR_SERVER: 
                mError = " server"; 
                break;
            case SpeechRecognizer.ERROR_CLIENT: 
                mError = " client"; 
                break;
            case SpeechRecognizer.ERROR_SPEECH_TIMEOUT: 
                mError = " speech time out" ; 
                break;
            case SpeechRecognizer.ERROR_NO_MATCH: 
                mError = " no match" ; 
                break;
            case SpeechRecognizer.ERROR_RECOGNIZER_BUSY: 
                mError = " recogniser busy" ; 
                break;
            case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS: 
                mError = " insufficient permissions" ; 
                break;

            }
            //Log.d(TAG,  "Error: " +  error + " - " + mError);
            //startListening();

        }

        @Override
        public void onEndOfSpeech() {
            // TODO Auto-generated method stub
            //Log.d(TAG, "onEndOfSpeech");
            //startListening();
        }

        @Override
        public void onBufferReceived(byte[] buffer) {
            // TODO Auto-generated method stub
            //Log.d(TAG, "onBufferReceived");
        }

        @Override
        public void onBeginningOfSpeech() {
            // TODO Auto-generated method stub
        }
    }
}

以下是我看到的输出 - 结果是异常的,它应该显示一次而不是3次.. enter image description here

1 个答案:

答案 0 :(得分:1)

以下是来自android上的google-speech-api之一的响应。请注意'假设'字段中的JSON数组......

  

{ “状态”:0, “ID”: “a4ca9654c6cc684dc3279cd1aaa00cc7-1”, “假设”:[{ “发声”:“地图   加利福尼亚州“,”置信度“:0.87869847}]}

您需要知道正在使用的api响应主体的详细信息,如有必要,还需要了解如何在响应中解析JSON数组,如上面的“假设”字段。

如果它是一个阵列,我怀疑它是,那么你只需要对阵列进行一点解析即可获得正确的响应,而不会出现重复问题。