Voice recognizer + Text to speech

Merge last two post; Start Android system's voice recognizer using Intent & Implement Android's Text to speech function.

Voice recognizer + Text to speech

Onec the recognized result selected, it will be speeched using TTS.

package com.AndroidRecognizer;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class AndroidRecognizer extends Activity implements OnInitListener{

TextToSpeech tts;

Button startRecognizer;
Spinner spinnerResult;

private static final int RQS_RECOGNITION = 1;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startRecognizer = (Button)findViewById(R.id.startrecognizer);
startRecognizer.setEnabled(false);

spinnerResult = (Spinner)findViewById(R.id.result);

startRecognizer.setOnClickListener(startRecognizerOnClickListener);

tts = new TextToSpeech(this, this);

}

private Button.OnClickListener startRecognizerOnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
"Speech to Recognize");
startActivityForResult(intent, RQS_RECOGNITION);
}
};

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if((requestCode == RQS_RECOGNITION) & (resultCode == RESULT_OK)){

ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, result);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerResult.setAdapter(adapter);

spinnerResult.setOnItemSelectedListener(spinnerResultOnItemSelectedListener);

}

}

private Spinner.OnItemSelectedListener spinnerResultOnItemSelectedListener
= new Spinner.OnItemSelectedListener(){

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
String selectedResult = parent.getItemAtPosition(position).toString();
Toast.makeText(AndroidRecognizer.this, selectedResult, Toast.LENGTH_SHORT).show();
tts.speak(selectedResult, TextToSpeech.QUEUE_ADD, null);

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}};

@Override
public void onInit(int arg0) {
// TODO Auto-generated method stub
startRecognizer.setEnabled(true);
}

}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/startrecognizer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start Recognizer"
/>
<Spinner
android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

0 Response to "Voice recognizer + Text to speech"

Posting Komentar