Skip to content

Instantly share code, notes, and snippets.

@brentonhouse
Forked from hansemannn/ti.speech.example.js
Created November 28, 2019 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brentonhouse/d96397042b1ce90adf1756cc76249b34 to your computer and use it in GitHub Desktop.
Save brentonhouse/d96397042b1ce90adf1756cc76249b34 to your computer and use it in GitHub Desktop.
Realtime Speech Recognition in Titanium
var TiSpeech = require('ti.speech');
TiSpeech.initialize('en_US'); // locale is optional
var win = Ti.UI.createWindow({
backgroundColor: '#fff'
});
var currentValue = '';
win.addEventListener('open', function () {
TiSpeech.requestSpeechRecognizerAuthorization(function (e) {
if (!e.success) {
alert(e.message);
}
});
});
var btn = Ti.UI.createButton({
title: 'Recognize real-time speech'
});
if (!TiSpeech.isAvailable()) {
alert('Speech recognition is not available on this device!');
btn.enabled = false;
}
btn.addEventListener('click', function() {
TiSpeech.requestMicrophoneAuthorization(function (e) {
if (!e.success) {
alert(e.message);
} else {
startRecognition();
}
})
});
win.add(btn);
win.open();
function startRecognition() {
TiSpeech.startRecognition({
// 1) Microphone based detection
type: TiSpeech.SOURCE_TYPE_MICROPHONE,
// 2) URL based detection
// type: TiSpeech.SOURCE_TYPE_URL,
// url: 'one_more_thing.mp3',
progress: function(e) {
if (currentValue === e.value) return;
currentValue = e.value;
Ti.API.info(currentValue);
if (currentValue.toLowerCase().indexOf('stop') !== -1) {
console.log('-- STOPPING --');
TiSpeech.stopRecognition();
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment