活动和语音识别

时间:2011-10-31 10:57:21

标签: java android voice-recognition

我有这段代码:

public class VoiceActivity extends Activity implements OnClickListener {

private TextView mText;
private SpeechRecognizer sr;
private static final String TAG = "MyActivity";
public String str;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button speakButton = (Button) findViewById(R.id.speakButton);
    mText = (TextView) findViewById(R.id.textView1);

    speakButton.setOnClickListener(this);
    sr = SpeechRecognizer.createSpeechRecognizer(this);
    sr.setRecognitionListener(new listener());
}

class listener implements RecognitionListener {
    public void onReadyForSpeech(Bundle params) {
        Log.d(TAG, "onReadyForSpeech");
    }

    public void onBeginningOfSpeech() {
        Log.d(TAG, "onBeginningOfSpeech");
    }

    public void onRmsChanged(float rmsdB) {
        Log.d(TAG, "onRmsChanged");
    }

    public void onBufferReceived(byte[] buffer) {
        Log.d(TAG, "onBufferReceived");
    }

    public void onEndOfSpeech() {
        Log.d(TAG, "onEndofSpeech");
    }

    public void onError(int error) {
        Log.d(TAG, "error " + error);
        mText.setText("error " + error);
    }

    public void onResults(Bundle results) {
        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);
        }

        mText.setText("results: " + str);

    }

    public void onPartialResults(Bundle partialResults) {
        Log.d(TAG, "onPartialResults");
    }

    public void onEvent(int eventType, Bundle params) {
        Log.d(TAG, "onEvent " + eventType);
    }
}

public void onClick(View v) {
    if (v.getId() == R.id.speakButton) {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
                RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "com.moc");
        intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
        sr.startListening(intent);
    }
}

}

如何在识别语音后自动转换到另一个活动(按下按钮 - 比如说 - 打开下一个活动并产生结果)?在我的例子中,它表示行中的错误

  

intent.setClass(this,SecondActivity.class)。

示例:

public void onResults(Bundle results) {
    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);
    }
    Intent intent = new Intent();
    Bundle b = new Bundle();
        b.putString("StrID", str);
        intent.putExtras(b);
    intent.setClass(this, SecondActivity.class);
    startActivity(intent);
}

1 个答案:

答案 0 :(得分:0)

您应该将this替换为VoiceActivity.this,因为this指的是listener实例而不是您活动的背景:

intent.setClass (VoiceActivity.this, SecondActivity.class);