How to use Baidu speech synthesis (TTS) SDK

Source: Internet
Author: User

Speech synthesis is a key technology required to achieve human-computer speech interaction and establish an interactive system with listening and speaking capabilities. This article describes how to use the Baidu Android speech synthesis SDK. Similar to the speech recognition SDK, you also need to register the SDK and enable the speech synthesis API service. For detailed steps, refer to the Registration Section in the open link. The Baidu speech synthesis SDK is released in the form of a JAR package + Dynamic Link Library. You need to click the open link to download the SDK and copy the libs folder to the project.
Apply for necessary permissions[Java]View plaincopy

  1. <Uses-permission android: name = "android. permission. INTERNET"> </uses-permission>
  2. <Uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE"> </uses-permission>
  3. <Uses-permission android: name = "android. permission. READ_PHONE_STATE"/>

Initialize the SpeechSynthesizer object [Java]View plaincopy
  1. SpeechSynthesizer = new SpeechSynthesizer (getApplicationContext (),
  2. "Holder", this );
  3. // Replace the two parameters of the setApiKey method with the apiKey and secretKey obtained by registering an application in the Baidu Developer Center.
  4. SpeechSynthesizer. setApiKey ("your-apiKey", "your-secretKey ");

Parameter settings [Java]View plaincopy
  1. Private void setParams (){
  2. SpeechSynthesizer. setParam (SpeechSynthesizer. PARAM_SPEAKER, "0 ");
  3. SpeechSynthesizer. setParam (SpeechSynthesizer. PARAM_VOLUME, "5 ");
  4. SpeechSynthesizer. setParam (SpeechSynthesizer. PARAM_SPEED, "5 ");
  5. SpeechSynthesizer. setParam (SpeechSynthesizer. PARAM_PITCH, "5 ");
  6. SpeechSynthesizer. setParam (SpeechSynthesizer. PARAM_AUDIO_ENCODE, "1 ");
  7. SpeechSynthesizer. setParam (SpeechSynthesizer. PARAM_AUDIO_RATE, "4 ");
  8. SpeechSynthesizer. setParam (SpeechSynthesizer. PARAM_LANGUAGE, "ZH ");
  9. SpeechSynthesizer. setParam (SpeechSynthesizer. PARAM_NUM_PRON, "0 ");
  10. SpeechSynthesizer. setParam (SpeechSynthesizer. PARAM_ENG_PRON, "0 ");
  11. SpeechSynthesizer. setParam (SpeechSynthesizer. PARAM_PUNC, "0 ");
  12. SpeechSynthesizer. setParam (SpeechSynthesizer. PARAM_BACKGROUND, "0 ");
  13. SpeechSynthesizer. setParam (SpeechSynthesizer. PARAM_STYLE, "0 ");
  14. SpeechSynthesizer. setParam (SpeechSynthesizer. PARAM_TERRITORY, "0 ");
  15. }
You do not need to implement so many parameters. For more information about the parameters, see the SDK User Guide and parameter settings. To set the audio stream for audio broadcasting, you can call the following interface: [Java]View plaincopy
  1. SpeechSynthesizer. setAudioStreamType (AudioManager. STREAM_MUSIC );
The above content can be used for speech synthesis. During the recognition process, SpeechSynthesizerListener can be used to monitor the status, thus better implementing interface synchronization. [Java]View plaincopy
  1. Class listener implements SpeechSynthesizerListener {
  2. @ Override
  3. Public void onStartWorking (SpeechSynthesizer synthesizer ){
  4. LogDebug ("start working, please wait for data ...");
  5. }
  6. @ Override
  7. Public void onSpeechStart (SpeechSynthesizer synthesizer ){
  8. LogDebug ("start reading ");
  9. }
  10. @ Override
  11. Public void onSpeechResume (SpeechSynthesizer synthesizer ){
  12. LogDebug ("continue reading ");
  13. }
  14. @ Override
  15. Public void onSpeechProgressChanged (SpeechSynthesizer synthesizer, int progress ){
  16. }
  17. @ Override
  18. Public void onSpeechPause (SpeechSynthesizer synthesizer ){
  19. LogDebug ("reading suspended ");
  20. }
  21. @ Override
  22. Public void onSpeechFinish (SpeechSynthesizer synthesizer ){
  23. LogDebug ("reading stopped ");
  24. }
  25. @ Override
  26. Public void onNewDataArrive (SpeechSynthesizer synthesizer, byte [] dataBuffer, int dataLength ){
  27. LogDebug ("new audio data:" + dataLength );
  28. }
  29. @ Override
  30. Public void onError (SpeechSynthesizer synthesizer, SpeechError error ){
  31. LogError ("error:" + error. errorDescription + "(" + error. errorCode + ")");
  32. }
  33. @ Override
  34. Public void onCancel (SpeechSynthesizer synthesizer ){
  35. LogDebug ("canceled ");
  36. }
  37. @ Override
  38. Public void onBufferProgressChanged (SpeechSynthesizer synthesizer, int progress ){
  39. }
  40. }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.