Created
March 7, 2017 08:54
-
-
Save EddyVerbruggen/f3509c1f338194b0f09298fde7bbff23 to your computer and use it in GitHub Desktop.
Speech Recognition in NativeScript
This file contains 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
// import the plugin | |
import { SpeechRecognition } from "nativescript-speech-recognition"; | |
class SpeechRecognition { | |
// instantiate the plugin | |
private speechRecognition = new SpeechRecognition(); | |
public checkAvailability(): void { | |
this.speechRecognition.available().then( | |
(available: boolean) => console.log(available ? "YES!" : "NO"), | |
(err: string) => console.log(err) | |
); | |
} | |
public startListening(): void { | |
this.speechRecognition.startListening({ | |
// optional, uses the device locale by default | |
locale: "en-US", | |
// this callback will be invoked repeatedly during recognition | |
onResult: (transcription: SpeechRecognitionTranscription) => { | |
console.log(`User said: ${transcription.text}`); | |
console.log(`User finished?: ${transcription.finished}`); | |
}, | |
}).then( | |
(started: boolean) => { console.log(`started listening`) }, | |
(errorMessage: string) => { console.log(`Error: ${errorMessage}`); } | |
); | |
} | |
public stopListening(): void { | |
this.speechRecognition.stopListening().then( | |
() => { console.log(`stopped listening`) }, | |
(errorMessage: string) => { console.log(`Stop error: ${errorMessage}`); } | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment