Quantcast
Viewing latest article 6
Browse Latest Browse All 10

voice command example using proximity sensor, voice recognizer and TTS in android

In this example we will combine some features of android like proximity sensor, voice recognizer and TTS.

Proximity Sensor: is sensor that can recognize presence of nearby object. we will user this sensor to start voice recognition. So when you take you android device near to your face, proximity sensor will start the voice recognizer.

Voice Recognizer: can convert speech to text. So when you give the voice command, its will return an array of matches and will start TTS.

TTS(Text To Speech): can convert text speech. In this application we will use TTS to listen voice command response.

1. First we will check to see if proximity sensor is available.

SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		Sensor proximitySensor = sensorManager
				.getDefaultSensor(Sensor.TYPE_PROXIMITY);

		if (proximitySensor == null) {
			Toast.makeText(this, "No proximity sensor", Toast.LENGTH_SHORT)
					.show();
		} else {
			sensorManager.registerListener(proximitySensorEventListener,
					proximitySensor, SensorManager.SENSOR_DELAY_NORMAL);
		}

2. We will catch proximity event in onSensorChanged callback method in proximitySensorEventListener.

SensorEventListener proximitySensorEventListener = new SensorEventListener() {

		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onSensorChanged(SensorEvent event) {
			if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
				if (event.values[0] < proximitySensor.getMaximumRange()) {
					startSpeechRecognizer();
				}

			}
		}
	};

3. Start speech recognizer, when any object comes near to mobile.

private static final int VOICE_REQUEST_CODE = 101;
private void startSpeechRecognizer() {
		PackageManager pm = getPackageManager();
		List activities = pm.queryIntentActivities(new Intent(
				RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
		if (activities.size() != 0) {
		Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
		intent.putExtra(RecognizerIntent.ACTION_RECOGNIZE_SPEECH,
				RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
		intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Voice Command");
		startActivityForResult(intent, VOICE_REQUEST_CODE);
		}else{
			Toast.makeText(this, "Speech Recognizer is not present", Toast.LENGTH_SHORT)
			.show();
		}

	}

Explanation of above code:
queryIntentActivities: will return the list of activities that can handle action ACTION_RECOGNIZE_SPEECH.
ACTION_RECOGNIZE_SPEECH: start implicit activity that can handle action ACTION_RECOGNIZE_SPEECH.
EXTRA_PROMPT: It will show custom message "Voice Command" on voice recognizer activity.
Image may be NSFW.
Clik here to view.
Voice Command

4. We will get result of speech recognizer in onActivityResult callback.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == VOICE_REQUEST_CODE && resultCode == RESULT_OK) {
			ArrayList matches = data
					.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
			for (int i = 0; i < commands.length; i++) {
				if (matches.contains(commands[i])) {
					selectedCommand = i;
				}
			}
			if (isTtsInitialized) {
				if (selectedCommand >= 0) {
					tts.speak("you have selected" + commands[selectedCommand]
							+ " command.", TextToSpeech.QUEUE_FLUSH, null);
				} else {
					tts.speak("I am sorry. could you please repeat that.", TextToSpeech.QUEUE_FLUSH, null);
				}

			}

		}
}

Explanation of above code:

requestCode and resultCode: first we will check requestCode and resultCode to insure that onActivityResult is called by speech recognizer with success response.
getStringArrayListExtra: We will get all matches using getStringArrayListExtra. If matches contains our commands, TTS will say that otherwise it will say "I am sorry. could you please repeat that.".

5. We will check to see if TTS is present. First we need to implement OnInitListener and override onInit method.

@Override
	public void onInit(int status) {
		if (status == TextToSpeech.SUCCESS) {
			int result = tts.setLanguage(Locale.US);
			if (result == TextToSpeech.LANG_MISSING_DATA
					|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
				Toast.makeText(this, "Language is not available",
						Toast.LENGTH_SHORT);
			} else {
				isTtsInitialized = true;
			}
		} else {
			Toast.makeText(this, "Initialization failed", Toast.LENGTH_SHORT);
		}

	}

After completion of TTS engine initialization start the TTs.

Intent checkIntent = new Intent();
		checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
		startActivityForResult(checkIntent, TTS_REQUEST_CODE);

6. We will get response in onActivityResult.

@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		if (requestCode == TTS_REQUEST_CODE) {
			if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
				// success, create the TTS instance
				tts = new TextToSpeech(this, this);
			} else {
				// missing data, install it
				Intent installIntent = new Intent();
				installIntent
						.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
				startActivity(installIntent);
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

7. Don't forget to unregister proximity sensor listener.

@Override
	protected void onStop() {
		if (sensorManager != null) {
			sensorManager.unregisterListener(proximitySensorEventListener);
		}
		super.onStop();
	}

Instead of just listening the commands we can do lots of things like opening address book, messages, mails, etc. Here is the complete code.

package com.vimviv;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.widget.Toast;

public class VoiceCommandActivity extends Activity implements OnInitListener {

	private static final int VOICE_REQUEST_CODE = 101;
	private static final int TTS_REQUEST_CODE = 102;
	private static final String[] commands = { "open", "close", "find" };

	private int selectedCommand = -1;
	private String speachMessage = "I am sorry. could you please repeat that.";
	private boolean isTtsInitialized = false;
	TextToSpeech tts;
	SensorManager sensorManager;
	Sensor proximitySensor;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// check proximity sensor
		sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		proximitySensor = sensorManager
				.getDefaultSensor(Sensor.TYPE_PROXIMITY);

		if (proximitySensor == null) {
			Toast.makeText(this, "No proximity sensor", Toast.LENGTH_SHORT)
					.show();
		} else {
			sensorManager.registerListener(proximitySensorEventListener,
					proximitySensor, SensorManager.SENSOR_DELAY_NORMAL);
		}
		
		//check for TTS data
		Intent checkIntent = new Intent();
		checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
		startActivityForResult(checkIntent, TTS_REQUEST_CODE);

	}

	SensorEventListener proximitySensorEventListener = new SensorEventListener() {

		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onSensorChanged(SensorEvent event) {
			// TODO Auto-generated method stub

			if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
				if (event.values[0] < proximitySensor.getMaximumRange()) {
					startSpeachRecognition();
				}

			}
		}
	};

	private void startSpeachRecognition() {
		// Check to see if a recognition activity is present
				PackageManager pm = getPackageManager();
				List activities = pm.queryIntentActivities(new Intent(
						RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
				if (activities.size() != 0) {
					Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
					intent.putExtra(RecognizerIntent.ACTION_RECOGNIZE_SPEECH,
							RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
					intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Voice Command");
					startActivityForResult(intent, VOICE_REQUEST_CODE);
				} else {
					Toast.makeText(this, "Recognizer not present", Toast.LENGTH_SHORT)
					.show();
				}

		

	}
	
	// Initialization response of TTS engine
	@Override
	public void onInit(int status) {
		if (status == TextToSpeech.SUCCESS) {
			int result = tts.setLanguage(Locale.US);
			if (result == TextToSpeech.LANG_MISSING_DATA
					|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
				Toast.makeText(this, "Language is not available",
						Toast.LENGTH_SHORT);
			} else {
				isTtsInitialized = true;
			}
		} else {
			Toast.makeText(this, "Initialization failed", Toast.LENGTH_SHORT);
		}

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == VOICE_REQUEST_CODE && resultCode == RESULT_OK) {
			ArrayList matches = data
					.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
			for (int i = 0; i < commands.length; i++) {
				if (matches.contains(commands[i])) {
					selectedCommand = i;
				}
			}
			if (isTtsInitialized) {
				if (selectedCommand >= 0) {
					tts.speak("you have selected" + commands[selectedCommand]
							+ " command.", TextToSpeech.QUEUE_FLUSH, null);
					selectedCommand = -1;
				} else {
					tts.speak(speachMessage, TextToSpeech.QUEUE_FLUSH, null);
				}

			}

		}
		if (requestCode == TTS_REQUEST_CODE) {
			if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
				// success, create the TTS instance
				tts = new TextToSpeech(this, this);
			} else {
				// missing data, install it
				Intent installIntent = new Intent();
				installIntent
						.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
				startActivity(installIntent);
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void onStop() {
		if (sensorManager != null) {
			sensorManager.unregisterListener(proximitySensorEventListener);
		}
		super.onStop();
	}

	
}

Incoming search terms:


Viewing latest article 6
Browse Latest Browse All 10

Trending Articles