Android SDK (Legacy)

Updated at:

This topic describes the key interfaces, call sequence, and speech synthesis examples for applications that already use the legacy Android SDK.

Important

The legacy Android SDK is no longer updated. We recommend using the latest SDK. For more information, see Android SDK.

Prerequisites

  • Integrate the legacy Android SDK into the application.

  • Obtain a project Appkey. For more information, see Manage projects.

  • Obtain an access token. For more information, see Obtain an access token.

For speech synthesis parameters and limits, see API reference.

Key interfaces

  • NlsClient: A thread-safe client for short sentence recognition, real-time speech recognition, and speech synthesis. We recommend creating a single global instance.

  • SpeechSynthesizer: Represents a speech synthesis request.

  • SpeechSynthesizerCallback: The callback interface for speech synthesis. Implement this interface to handle synthesized audio, timestamps, synthesis completion, and errors.

Call sequence

  1. Create an NlsClient instance.

  2. Implement SpeechSynthesizerCallback to handle synthesized audio and errors.

  3. Call NlsClient.createSynthesizerRequest(callback) to create a SpeechSynthesizer instance.

  4. Set the token, Appkey, text, voice, speech rate, and other parameters.

  5. Call SpeechSynthesizer.start() to start synthesis.

  6. Receive and play the audio data in onBinaryReceived.

  7. Call SpeechSynthesizer.stop() in onSynthesisCompleted or onTaskFailed to end the request. Repeat steps 3 to 7 for a new request.

  8. Call NlsClient.release() when the client is no longer needed.

ProGuard configuration

If code obfuscation is enabled, add the following rule to proguard-rules.pro:

-keep class com.alibaba.idst.util.*{*;}

Code examples

The following snippets are for an existing Android application. Import NlsClient, SpeechSynthesizer, and SpeechSynthesizerCallback from com.alibaba.idst.util, and import android.util.Log. The callback variable refers to the application’s SpeechSynthesizerCallback implementation. Add the callback methods below to that implementation. The speechSynthesizer variable holds the current request.

Initialize client with new NlsClient() and reuse it. The application must initialize audioTrack to play 16 kHz PCM audio that matches the request settings. Set playing to false before each playback. The TAG variable is the log tag.

Create a synthesis request

Replace <TOKEN> and <APPKEY> with the values obtained earlier. The example uses PCM audio, which can be played directly with AudioTrack. Other encoded formats cannot be played directly as PCM.

speechSynthesizer = client.createSynthesizerRequest(callback);
speechSynthesizer.setToken("<TOKEN>");
speechSynthesizer.setAppkey("<APPKEY>");

speechSynthesizer.setFormat(SpeechSynthesizer.FORMAT_PCM);
speechSynthesizer.setSampleRate(SpeechSynthesizer.SAMPLE_RATE_16K);
speechSynthesizer.setVoice(SpeechSynthesizer.VOICE_XIAOGANG);
speechSynthesizer.setMethod(SpeechSynthesizer.METHOD_RUS);
speechSynthesizer.setSpeechRate(100);
speechSynthesizer.setEnableSubtitle(true);
speechSynthesizer.setText("Welcome to Intelligent Speech Interaction!");

if (speechSynthesizer.start() < 0) {
    speechSynthesizer.stop();
    Log.e(TAG, "Failed to start speech synthesis");
}

Receive and play audio

@Override
public void onBinaryReceived(byte[] data, int code) {
    if (!playing) {
        playing = true;
        audioTrack.play();
    }
    audioTrack.write(data, 0, data.length);
}

Receive timestamps

Set setEnableSubtitle(true) to receive speech timestamp information through onMetaInfo.

@Override
public void onMetaInfo(String message, int code) {
    Log.d(TAG, "onMetaInfo " + message + ": " + code);
}

End the request and release the client

End the current request when synthesis completes or fails.

@Override
public void onSynthesisCompleted(String message, int code) {
    speechSynthesizer.stop();
}

@Override
public void onTaskFailed(String message, int code) {
    Log.e(TAG, message + ": " + code);
    speechSynthesizer.stop();
}

Call client.release() when no more speech tasks are needed. The application manages the audio player resources.