How do I voice-enable Android applications with SpeechKit?

Last reviewed: 7/8/2022

HOW Article ID: H072214

The information in this article applies to:

  • SpeechKit 11

Summary

Voice enabling Android applications is easier with SpeechKit classes for managing speech recognition and speech synthesis.

More Information

Develop Android applications that speak and listen using your favorite Java Android development tools. This includes development environments such as Android Studio.

The following sections describe the steps for integrating SpeechKit with Java Android applications.

SpeechKit Jars

SpeechKit includes Android compatible jar libraries.

To access the SpeechKit classes within your application, add them to your project:

  1. Program Files\Chant\SpeechKit 11\Android\lib\speechkit.jar and
  2. Program Files\Chant\SpeechKit 11\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.*;

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 JAndroidRecognizer _Recognizer = null;
    private JAndroidSynthesizer _Synthesizer = null;
    static MainActivity _Instance = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        _Instance = this;
        // Create SpeechKit object
        _SpeechKit = new JSpeechKit();
        // Set credentials
        _SpeechKit.setLicense("Credentials");

        // Verify permission
        verifySpeechPermissions(_Instance);
        _Recognizer = _SpeechKit.createAndroidRecognizer();
        if (_Recognizer != null)
        {
            // Set the callback object
            _Recognizer.setChantSpeechKitEvents(this);
            // Register for callbacks
            _Recognizer.registerCallback(ChantSpeechKitCallback.CCSRRecognitionCommand);
            // Set app context
            _Recognizer.setContext(getApplicationContext());
        }

        _Synthesizer = _SpeechKit.createAndroidSynthesizer();
        if (_Synthesizer != null)
        {
            // Set the callback object
            _Synthesizer.setChantSpeechKitEvents(this);
            // Register for callbacks
            _Synthesizer.registerCallback(ChantSpeechKitCallback.CCTTSInitComplete);
            // Set app context
            _Synthesizer.setContext(getApplicationContext());
        }
    }
}

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" />

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());
        }
    }
    ...
}