Created
April 8, 2019 05:29
-
-
Save hansemannn/b5b867b73f9e662bf828e3e2aa24f045 to your computer and use it in GitHub Desktop.
Realtime Speech Recognition in Titanium
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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