All Products
Search
Document Center

Android SDK 2.0

Last Updated: Jun 02, 2020

Notes:

Download and installation

  1. Download the Android SDK and sample code.
  2. Decompress the downloaded package and find the demo project in the nls-sdk-android directory. In the app/libs directory, you can find an .aar file, which is the SDK package.
  3. Open the demo project in Android Studio. The sample code for speech synthesis is included in the SpeechSynthesizerActivity object:
  4. Before running the sample code, create a project and obtain the project appkey in the Intelligent Speech Interaction console, and obtain the service token. For more information, see relevant documentation.

Key objects

  • NlsClient: the speech processing client, which is equivalent to a factory for all speech processing classes. You can globally create an NlsClient instance.
  • SpeechSynthesizer: the speech synthesis object, representing a speech synthesis request.
  • SpeechSynthesizerCallback: the object of speech synthesis callback functions. These callbacks are fired when synthesized audio data is returned or any errors occur. You can customize these callbacks with your own logic.

Call sequence

  1. Create an NlsClient instance.
  2. Define callbacks of the SpeechSynthesizerCallback object to process recognition results and errors as required.
  3. Call the NlsClient.createSynthesizerRequest() method to create a SpeechSynthesizer instance.
  4. Set SpeechSynthesizer parameters, including accessToken and appkey.
  5. Other parameters include the text to be synthesized (text), the speaker (voice), the speed (speechRate), and parameters that affect the voice effect.
  6. Call the SpeechSynthesizer.start() method to connect the server.
  7. Obtain and play the synthesized audio data in the callback, or handle errors.
  8. Call the SpeechSynthesizer.stop() method to complete speech synthesis.
  9. To initiate a new request, repeat steps 3 to 8.
  10. Call the NlsClient.release() method to release the client instance.

ProGuard configuration

If you use the obfuscating code, configure the following command line in the proguard-rules.pro file:

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

Sample code

Create a recognition request.

  1. // Create a speech synthesis object.
  2. speechSynthesizer = client.createSynthesizerRequest(callback);
  3. speechSynthesizer.setUrl("wss://nls-gateway-ap-southeast-1.aliyuncs.com/ws/v1");
  4. // Obtain a dynamic token. For more information, see https://www.alibabacloud.com/help/doc-detail/72153.htm.
  5. speechSynthesizer.setToken("");
  6. // Obtain an appkey in the Intelligent Speech Interaction console (https://nls-portal.console.aliyun.com/).
  7. speechSynthesizer.setAppkey("");
  8. // Set the speech coding format. You can use audioTrack to play PCM files only.
  9. speechSynthesizer.setFormat(SpeechSynthesizer.FORMAT_PCM);
  10. // Different values of the following parameter change the final speech synthesis effect. You can modify the audition effect as instructed in the document.
  11. // Set the audio sampling rate.
  12. speechSynthesizer.setSampleRate(SpeechSynthesizer.SAMPLE_RATE_16K);
  13. // Set the speaker.
  14. speechSynthesizer.setVoice(SpeechSynthesizer.VOICE_XIAOGANG);
  15. // Set the speech synthesis method.
  16. speechSynthesizer.setMethod(SpeechSynthesizer.METHOD_RUS);
  17. // Set the speed.
  18. speechSynthesizer.setSpeechRate(100);
  19. // Set whether to return the timestamp information corresponding to the voice
  20. speechSynthesizer.setEnableSubtitle(true);
  21. // Set the text to be converted to speech.
  22. speechSynthesizer.setText("Thank you for choosing Intelligent Speech Interaction.") ;
  23. speechSynthesizer.start()

Obtain and play the synthesized audio file.

  1. // Obtain the audio data callback, and write the audio data to the player.
  2. @Override
  3. public void OnBinaryReceived(byte[] data, int code)
  4. {
  5. Log.d(TAG, "binary received length: " + data.length);
  6. if (!playing) {
  7. playing = true;
  8. audioTrack.play();
  9. }
  10. audioTrack.write(data, 0, data.length);
  11. }

Return voice timestamp information.

  1. // When onMetaInfo is called, it needs to be set as follows: SpeechSynthesizer.setEnableSubtitle(true)
  2. @Override
  3. public void onMetaInfo(String message, int code) {
  4. Log.d(TAG,"onMetaInfo " + message + ": " + String.valueOf(code));
  5. }