This topic describes how to use the legacy Android software development kit (SDK) for Alibaba Cloud Voice Service. It covers how to install the SDK and provides sample code. We recommend that new users use the latest Android SDK.
We recommend that you use the latest version of the Android SDK. This legacy version is no longer updated. For more information, see Android SDK.
Prerequisites
Read the API reference. For more information, see API reference.
Obtain a project appkey. For more information, see Create a project.
Obtain an access token for Voice Service. For more information, see Token overview.
Key interfaces
NlsClient: The speech processing client. You can use this client for speech processing tasks, such as short sentence recognition, real-time speech recognition, and speech synthesis. This client is thread-safe. We recommend that you create only one global instance.
SpeechSynthesizer: Represents a speech synthesis request.
SpeechSynthesizerCallback: The speech synthesis callback interface. Callbacks are triggered for events, such as receiving synthesized audio data or encountering errors. You must implement this interface and add your processing logic to the callback methods.
Call procedure
Create an NlsClient instance.
Define the SpeechSynthesizerCallback implementation class to handle recognition results or errors.
Call the NlsClient.createSynthesizerRequest() method to obtain a SpeechSynthesizer instance.
Set the SpeechSynthesizer parameters.
These parameters include the access token, appkey, text, voice, and speechRate.
Call the SpeechSynthesizer.start() method to start the service connection.
In the callback, retrieve and play the synthesized audio data, or handle errors.
Call the SpeechSynthesizer.stop() method to end the speech synthesis.
NoteTo start a new request, repeat Steps 3 to 7.
Call the NlsClient.release() method to release the client instance.
Proguard configuration
If you use code obfuscation, add the following configuration to the proguard-rules.pro file:
-keep class com.alibaba.idst.util.*{*;}Code examples
Make a recognition request
// Create a speech synthesis object. speechSynthesizer = client.createSynthesizerRequest(callback); speechSynthesizer.setToken(""); speechSynthesizer.setAppkey(""); // Set the audio encoding. AudioTrack can directly play audio in the PCM format, but not in other formats. speechSynthesizer.setFormat(SpeechSynthesizer.FORMAT_PCM); // The following options change the final synthesized speech. // Set the audio sample rate. speechSynthesizer.setSampleRate(SpeechSynthesizer.SAMPLE_RATE_16K); // Set the voice. speechSynthesizer.setVoice(SpeechSynthesizer.VOICE_XIAOGANG); // Set the speech synthesis method. speechSynthesizer.setMethod(SpeechSynthesizer.METHOD_RUS); // Set the speech rate. speechSynthesizer.setSpeechRate(100); // Specify whether to return timestamp information for the synthesized speech. speechSynthesizer.setEnableSubtitle(true); // Set the text to synthesize. speechSynthesizer.setText("Welcome to Voice Service!"); speechSynthesizer.start()Retrieve and play the synthesized speech
// In the callback for audio data, write the audio to the player. @Override public void OnBinaryReceived(byte[] data, int code) { Log.d(TAG, "binary received length: " + data.length); if (!playing) { playing = true; audioTrack.play(); } audioTrack.write(data, 0, data.length); }Retrieve speech timestamp information
// To receive the onMetaInfo callback, set SpeechSynthesizer.setEnableSubtitle(true). @Override public void onMetaInfo(String message, int code) { Log.d(TAG,"onMetaInfo " + message + ": " + String.valueOf(code)); }