Skip to content

Instantly share code, notes, and snippets.

@shouse
Created November 13, 2017 16:37
Show Gist options
  • Save shouse/f29c4fcd42dd6be356b9adb7676f772c to your computer and use it in GitHub Desktop.
Save shouse/f29c4fcd42dd6be356b9adb7676f772c to your computer and use it in GitHub Desktop.
Speech Recognition using Appcelerator Titanium and Hyperloop
'use strict';
/**
* Android sample showing speech recognition using Hyperloop.
*
* @version Titanium 6.1.2.GA
* @version Alloy 1.9.13
* @version Hyperloop 2.1.3
*/
var SpeechRecognizer = require('android.speech.SpeechRecognizer');
var RecognitionListener = require('android.speech.RecognitionListener');
var Intent = require('android.content.Intent');
var RecognizerIntent = require('android.speech.RecognizerIntent');
var Activity = require('android.app.Activity');
var ArrayList = require('java.util.ArrayList');
var ctx = Ti.Android.currentActivity;
/**
* Check if speech recognition is available.
* Main "Google" app required for speech recognition.
* @link https://play.google.com/store/apps/details?id=com.google.android.googlequicksearchbox
*/
if(!SpeechRecognizer.isRecognitionAvailable(ctx)) {
alert('Speech Recognition not available.')
}
var instance = SpeechRecognizer.createSpeechRecognizer(ctx);
// Set event callbacks.
instance.setRecognitionListener(new RecognitionListener({
onReadyForSpeech: function(e) {
console.log('onReadyForSpeech');
Ti.API.info(e);
},
onBeginningOfSpeech: function() {
console.log('onBeginningOfSpeech');
},
onEndOfSpeech: function() {
console.log('onEndOfSpeech');
},
/**
* @param bundle Contains an array of matched words. First word is usually the intended one.
*/
onResults: function(bundle) {
console.log('onResults');
// Cast to array since the parameter is not useable in its current form (probably a neater way to do this?)
var array = new ArrayList(bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)).toArray();
// Assume the first word is a color and lowercase it.
var word = array[0].toLowerCase();
// Set color on main window.
$.mainWindow.backgroundColor = word;
// Set word on main button to show the word that was recognized.
$.mainButton.title = word;
},
onError: function(e) {
console.log('onError');
Ti.API.info(e);
},
onEvent: function(e) {
console.log('onEvent');
Ti.API.info(e);
},
onPartialResults: function(e) {
console.log('onPartialResults');
Ti.API.info(e);
}
}));
var intent;
/**
* Click listener attached to a button on the screen. Creates an intent and starts listening for speech. The listener
* automatically stops when speech is no longer recognized.
*/
$.mainButton.addEventListener('click', function() {
intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, 'en_US');
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, 'Please speak now...');
// Assign Calling Package as current package name (not sure if required).
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, 'com.speechtest');
instance.startListening(intent);
});
// Open #mainWindow.
$.mainWindow.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment