Speech Recognition (Speech-to-Text)

API Reference: SpeechRecognizer

SpeechRecognizer provides access to the speech recognition service.

Offline Speech Recognition On Android

To use offline speech recognizer, language package must be installed. If not installed, the speech recognizer throws SpeechRecognizer.Error.SERVER error. This screenshot is taken from a device that supports English and Spanish speech recognition.

onFinish Callback

onFinish callback is triggered in a few seconds after starting Speech Recognizer on Android devices. But it requires much more time on iOS devices.

onResult Callback

onResult is fired when partial recognition results are available. This callback can also be fired different number of times in Android and iOS. There is no guarantee onResult callback will be fired on each word.

Network changes on iOS

When the network changes, speech recognizer may not available. The speech recognizer throws SpeechRecognizer.Error.NETWORK error in this case.

Supported Locales on iOS

For iOS, you should check your current locale is supported from isLocaleSupported function. if doesn't support your current locale, you must set locale parameter in constructor function.

Android Permissions

For Android, RECORD_AUDIO permission is required for using SpeechRecognizer. Add the following permission to AndroidManifest.xml file in necessary, also take a look at Application.android.Permissions.RECORD_AUDIO.

See also Android official documentation about this permission.

iOS Permissions

Permission keys must be added info.plist for iOS

NSSpeechRecognitionUsageDescription $(PRODUCT_NAME) Speech Recognition access for development

NSMicrophoneUsageDescription $(PRODUCT_NAME) Microphone access for development

JavaScript
JavaScript
const Page = require("sf-core/ui/page");
const FlexLayout = require("sf-core/ui/flexlayout");
const Button = require("sf-core/ui/button");
const Label = require("sf-core/ui/label");
const SpeechRecognizer = require("sf-core/speechrecognizer");
const Application = require("sf-core/application");
const System = require("sf-core/device/system");
const extend = require("js-base/core/extend");
var Page1 = new extend(Page)(
function Page1(_super, params) {
_super(this, {
onShow: function() {},
onLoad: function() {
var myFlexLayout = new FlexLayout({
flexGrow: 1,
flexDirection: FlexLayout.FlexDirection.COLUMN
});
var myButton = new Button({
flexGrow: 1,
text: "START SPEECH",
onPress: function() {
if (!SpeechRecognizer.isRunning()) {
myButton.text = "Stop Recording";
if (System.OS === "iOS") {
startSpeechRecognizer();
}
else if (System.OS === "Android") {
const RECORD_AUDIO_CODE = 1002;
Application.android.requestPermissions(RECORD_AUDIO_CODE, Application.android.Permissions.RECORD_AUDIO);
Application.android.onRequestPermissionsResult = function(e) {
if (e.requestCode === RECORD_AUDIO_CODE && e.result) {
startSpeechRecognizer();
}
};
}
}
else {
myButton.text = "Start Recording";
SpeechRecognizer.stop();
}
}
});
var myLabel = new Label({
flexGrow: 3,
text: "Result is here"
});
myFlexLayout.addChild(myButton);
myFlexLayout.addChild(myLabel);
function startSpeechRecognizer() {
SpeechRecognizer.start({
locale : "en_US",
onResult: function(result) {
myLabel.text = result;
},
onFinish: function(result) {
myButton.text = "Start Recording";
alert("Finish : " + result);
},
onError: function(error) {
myButton.text = "Start Recording";
alert("Error : " + error);
}
});
}
this.headerBar.leftItemEnabled = false;
this.layout.addChild(myFlexLayout);
}
});
}
);
module.exports = Page1;