如何对文本执行语音执行任务?

时间:2013-07-19 03:53:31

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

我正在尝试编写一个应用程序,允许我说出命令并让应用程序执行我所说的内容。但这是问题所在,我不想使用弹出GoogleVoice的RecognizerIntent。我想拥有自己的定制产品。任何人都可以给我一些帮助或提示,让我这样做吗?也许在我说了什么并执行任务后如何使用结果的帮助?

更新的代码:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

public class VoicingMain extends Activity implements OnClickListener {

ListView lv;    
private SpeechRecognizer sr;
private Intent srIntent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.voicing);
    lv = (ListView) findViewById(R.id.lvList);
    Button b = (Button) findViewById(R.id.bStartVoicing);
    b.setOnClickListener(this);
    boolean available = SpeechRecognizer.isRecognitionAvailable(this);
    Log.d("Speech", "available = " + available);
    sr = SpeechRecognizer.createSpeechRecognizer(this);
    sr.setRecognitionListener(new SpeechListener());

}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    srIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    srIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    srIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
            this.getPackageName());
    srIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 5);
    Log.d("speech", "button active");
    sr.startListening(srIntent);
    new CountDownTimer(3000, 1000) {

        @Override
        public void onTick(long arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onFinish() {
            // TODO Auto-generated method stub
            sr.stopListening();
        }

    }.start();
}

private class SpeechListener implements RecognitionListener {

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

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

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

    @Override
    public void onError(int arg0) {
        // TODO Auto-generated method stub
        Log.d("Speech", "onError");
    }

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

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

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

    @Override
    public void onResults(Bundle arg0) {
        // TODO Auto-generated method stub
        Log.d("Speech", "results");
    }

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

}

}

这是我到目前为止所拥有的

1 个答案:

答案 0 :(得分:1)

SO question显示了如何访问SpeechListener中的结果。这些结果可在Results()方法中找到。

至于执行任务的能力,您应该有某种处理程序,它将根据文本启动必要的服务。此处理程序可以使用意图与服务进行通信。例如,以下是您对用户说“呼叫Bob”的反应(基于上面的链接):

        @Override
        public void onResults(Bundle results) {
            ArrayList<String> voiceResults = results
                    .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
            if (voiceResults == null) {
                Log.e(TAG, "No voice results");
            } else {
                for (int i = 0; i < voiceResults.size(); ++i) {
                    if( voiceResults[i].contains("Call") )
                      for( int j = i+1; j < matches.size(); ++j )
                        if( voiceResults[j].contains("Bob") )
                        {
                          //Get Bob's phone number, using [this][2]
                          String bobNumber = "123 123 1234";
                          Intent callIntent = new Intent(Intent.ACTION_CALL);          
                          callIntent.setData(Uri.parse("tel:"+bobNumber));          
                          startActivity(callIntent);
                          break;
                        }
                }
            }
        }
相关问题