Last reviewed: 3/23/2024 10:20:24 AM
Android Java Applications
Develop Android applications that speak and listen using your favorite Android Java development tools. This includes development environments such as Android Studio.
The following sections describe the steps for integrating SpeechKit with Android Java applications.
SpeechKit Jars
SpeechKit includes Android compatible jar libraries.
To access the SpeechKit classes within your application, add them to your project:
- Program Files\Chant\SpeechKit 13\Android\lib\speechkit.jar and
- Program Files\Chant\SpeechKit 13\Android\lib\chant.shared.jar.
To access the SpeechKit Java classes within your application, add references to the Chant shared and SpeechKit class libraries in your code:
import com.speechKit.*;
import net.chant.shared.*;
Azure Jar and Deployment Libraries
For Azure Speech, right click the Libraries folder in your Android Target Platform and select the Add... option to add the Azure Speech SDK classes to your project:
- Select File->Project Structure
- Select Dependencies->app
- Click + (Add Dependency)->Library Dependency
- Enter com.microsoft.cognitiveservices.speech:client-sdk and press the Search button
- Select the Azure SDK
- Press the OK button
Object Instantiation
Instantiate SpeechKit and set the credentials. For speech recognition, verify permissions, instantiate object, set callback event handler, and set application context. For speech synthesis, instantiate object, set callback event handler, and set application context.
public class MainActivity extends AppCompatActivity implements com.speechkit.JChantSpeechKitEvents
{
private JSpeechKit _SpeechKit = null;
private JChantRecognizer _Recognizer = null; // Platform default - Android Speech
//private JMCSRecognizer _Recognizer = null; // Azure Speech
private JChantSynthesizer _Synthesizer = null; // Platform default - Android Speech
//private JMCSSynthesizer _Synthesizer = null; // Azure Speech
static MainActivity _Instance = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
_Instance = this;
// Create SpeechKit object
_SpeechKit = new JSpeechKit();
// Set credentials
_SpeechKit.setCredentials("Credentials");
// Verify permission
verifySpeechPermissions(_Instance);
_Recognizer = _SpeechKit.createChantRecognizer();
//_Recognizer = _SpeechKit.createMCSRecognizer("speechKey", "speechRegion");
if (_Recognizer != null)
{
// Set the callback object
_Recognizer.setChantSpeechKitEvents(this);
// Register for callbacks
_Recognizer.registerCallback(ChantSpeechKitCallback.CCSRRecognitionCommand);
// Set app context
_Recognizer.setContext(getApplicationContext());
// For Azure Speech, set Activity for callbacks on UI thread
//_Recognizer.setActivity(this);
}
_Synthesizer = _SpeechKit.createChantSynthesizer();
//_Synthesizer = _SpeechKit.createMCSSynthesizer("speechKey", "speechRegion");
if (_Synthesizer != null)
{
// Set the callback object
_Synthesizer.setChantSpeechKitEvents(this);
// Register for callbacks
_Synthesizer.registerCallback(ChantSpeechKitCallback.CCTTSInitComplete);
// Set app context - Required for Android Speech, Optional for Azure Speech
_Synthesizer.setContext(getApplicationContext());
// For Azure Speech, set Activity for callbacks on UI thread
//_Synthesizer.setActivity(this);
}
}
}
Permissions
Speech recognition requires the user to grant RECORD_AUDIO permission. Add the appropriate permission in the manifest file and invoke permission inquiry in the application.
<uses-permission android:name="android.permission.RECORD_AUDIO" />
Speech synthesis streamed to a file requires WRITE_EXTERNAL_STORAGE permission. Add the appropriate permission in the manifest file and invoke permission inquiry in the application.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Apps Targeting Android 11
Speech recognition apps targeting Android 11 with API 30 require an additional manifest entry. Add the following queries section before the application element.
<queries>
<intent>
<action android:name="android.speech.RecognitionService" />
</intent>
</queries>
Speech synthesis apps targeting Android 11 with API 30 require an additional manifest entry. Add the following queries section before the application element.
<queries>
<intent>
<action android:name="android.intent.action.TTS_SERVICE" />
</intent>
</queries>
Event Callbacks
Event callbacks are the mechanism in which the class object sends information back to the application such as speech recognition occurred, audio playback finished, or there was an error.
...
@Override
public void recognitionDictation(Object o, RecognitionDictationEventArgs recognitionDictationEventArgs) {
if ((recognitionDictationEventArgs != null) && (recognitionDictationEventArgs.getText() != null)) {
...
}
}
...
@Override
public void initComplete(Object o, TTSEventArgs ttsEventArgs) {
if (_Synthesizer.getChantEngines() != null)
{
for (JChantEngine engine : _Synthesizer.getChantEngines())
{
// Add name to list
_Engines.add(engine.getName());
}
}
...
}
Development and Deployment Checklist
When developing and deploying Android Java applications, ensure you have a valid license, and bundle the correct Chant class libraries. Review the following checklist before developing and deploying your applications:
- Develop and deploy Java applications to any system with a valid license from Chant. See the section License for more information about licensing Chant software.
- Bundle the speechKit.jar and chant.shared.jar in the Android app.
Sample Projects
Java sample projects are installed at the following location:
- Documents\Chant\SpeechKit 13\Android.