Package com.oorian.html.js.speech
Class SpeechApi
java.lang.Object
com.oorian.html.js.JavaScriptApi
com.oorian.html.js.speech.SpeechApi
Provides access to Web Speech API (Text-to-Speech and Speech Recognition) from server-side Java code.
This API wraps two browser APIs:
- Speech Synthesis: Converts text to spoken audio
- Speech Recognition: Converts spoken audio to text
Usage - Text-to-Speech:
public class MyPage extends HtmlPage {
@Override
protected void createBody(Body body) {
// Simple text-to-speech
SpeechApi.speak("Hello, welcome to our application!");
// With options
SpeechApi.speak("Important message", "en-US", 1.0f, 1.0f);
}
}
Usage - Speech Recognition:
public class MyPage extends HtmlPage implements SpeechListener {
@Override
protected void createBody(Body body) {
body.registerListener(this, SpeechResultEvent.class);
body.registerListener(this, SpeechErrorEvent.class);
Button btn = new Button("Start Listening");
btn.registerListener(this, MouseClickedEvent.class);
body.addElement(btn);
}
@Override
public void onEvent(MouseClickedEvent event) {
SpeechApi.startRecognition(getBody(), "en-US");
}
@Override
public void onEvent(SpeechResultEvent event) {
System.out.println("You said: " + event.getTranscript());
}
@Override
public void onEvent(SpeechErrorEvent event) {
showMessage("Speech recognition error: " + event.getMessage());
}
}
- Author:
- Marvin P. Warble Jr.
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidCancels any ongoing speech using the current page context.static voidcancelSpeech(HtmlPage page) Cancels any ongoing speech.static voidPauses ongoing speech using the current page context.static voidpauseSpeech(HtmlPage page) Pauses ongoing speech.static voidResumes paused speech using the current page context.static voidresumeSpeech(HtmlPage page) Resumes paused speech.static voidSpeaks the given text.static voidSpeaks the given text with options.static voidSpeaks the given text using the current page context.static voidSpeaks the given text with options using the current page context.static voidstartRecognition(Element element) Starts speech recognition using the current page context.static voidstartRecognition(Element element, String lang) Starts speech recognition with language using the current page context.static voidstartRecognition(Element element, String lang, boolean continuous) Starts speech recognition with options using the current page context.static voidstartRecognition(HtmlPage page, Element element, String lang, boolean continuous) Starts speech recognition.static voidStops speech recognition using the current page context.static voidstopRecognition(HtmlPage page) Stops speech recognition.
-
Constructor Details
-
SpeechApi
public SpeechApi()
-
-
Method Details
-
speak
Speaks the given text using the current page context.- Parameters:
text- The text to speak.
-
speak
Speaks the given text.- Parameters:
page- The page context.text- The text to speak.
-
speak
Speaks the given text with options using the current page context.- Parameters:
text- The text to speak.lang- The language/locale (e.g., "en-US", "es-ES").rate- Speaking rate (0.1 to 10, default 1.0).pitch- Voice pitch (0 to 2, default 1.0).
-
speak
Speaks the given text with options.- Parameters:
page- The page context.text- The text to speak.lang- The language/locale (e.g., "en-US", "es-ES").rate- Speaking rate (0.1 to 10, default 1.0).pitch- Voice pitch (0 to 2, default 1.0).
-
cancelSpeech
public static void cancelSpeech()Cancels any ongoing speech using the current page context. -
cancelSpeech
Cancels any ongoing speech.- Parameters:
page- The page context.
-
pauseSpeech
public static void pauseSpeech()Pauses ongoing speech using the current page context. -
pauseSpeech
Pauses ongoing speech.- Parameters:
page- The page context.
-
resumeSpeech
public static void resumeSpeech()Resumes paused speech using the current page context. -
resumeSpeech
Resumes paused speech.- Parameters:
page- The page context.
-
startRecognition
Starts speech recognition using the current page context.- Parameters:
element- The element that will receive recognition events.
-
startRecognition
Starts speech recognition with language using the current page context.- Parameters:
element- The element that will receive recognition events.lang- The language/locale (e.g., "en-US").
-
startRecognition
Starts speech recognition with options using the current page context.- Parameters:
element- The element that will receive recognition events.lang- The language/locale (e.g., "en-US").continuous- True for continuous recognition, false for single result.
-
startRecognition
public static void startRecognition(HtmlPage page, Element element, String lang, boolean continuous) Starts speech recognition.- Parameters:
page- The page context.element- The element that will receive recognition events.lang- The language/locale (e.g., "en-US"), or null for default.continuous- True for continuous recognition, false for single result.
-
stopRecognition
public static void stopRecognition()Stops speech recognition using the current page context. -
stopRecognition
Stops speech recognition.- Parameters:
page- The page context.
-