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
- <Uses-permission android: name = "android. permission. INTERNET"> </uses-permission>
- <Uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE"> </uses-permission>
- <Uses-permission android: name = "android. permission. READ_PHONE_STATE"/>
Initialize the SpeechSynthesizer object
[Java]View plaincopy
- SpeechSynthesizer = new SpeechSynthesizer (getApplicationContext (),
- "Holder", this );
- // Replace the two parameters of the setApiKey method with the apiKey and secretKey obtained by registering an application in the Baidu Developer Center.
- SpeechSynthesizer. setApiKey ("your-apiKey", "your-secretKey ");
Parameter settings
[Java]View plaincopy
- Private void setParams (){
- SpeechSynthesizer. setParam (SpeechSynthesizer. PARAM_SPEAKER, "0 ");
- SpeechSynthesizer. setParam (SpeechSynthesizer. PARAM_VOLUME, "5 ");
- SpeechSynthesizer. setParam (SpeechSynthesizer. PARAM_SPEED, "5 ");
- SpeechSynthesizer. setParam (SpeechSynthesizer. PARAM_PITCH, "5 ");
- SpeechSynthesizer. setParam (SpeechSynthesizer. PARAM_AUDIO_ENCODE, "1 ");
- SpeechSynthesizer. setParam (SpeechSynthesizer. PARAM_AUDIO_RATE, "4 ");
- SpeechSynthesizer. setParam (SpeechSynthesizer. PARAM_LANGUAGE, "ZH ");
- SpeechSynthesizer. setParam (SpeechSynthesizer. PARAM_NUM_PRON, "0 ");
- SpeechSynthesizer. setParam (SpeechSynthesizer. PARAM_ENG_PRON, "0 ");
- SpeechSynthesizer. setParam (SpeechSynthesizer. PARAM_PUNC, "0 ");
- SpeechSynthesizer. setParam (SpeechSynthesizer. PARAM_BACKGROUND, "0 ");
- SpeechSynthesizer. setParam (SpeechSynthesizer. PARAM_STYLE, "0 ");
- SpeechSynthesizer. setParam (SpeechSynthesizer. PARAM_TERRITORY, "0 ");
- }
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
- 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
- Class listener implements SpeechSynthesizerListener {
- @ Override
- Public void onStartWorking (SpeechSynthesizer synthesizer ){
- LogDebug ("start working, please wait for data ...");
- }
- @ Override
- Public void onSpeechStart (SpeechSynthesizer synthesizer ){
- LogDebug ("start reading ");
- }
- @ Override
- Public void onSpeechResume (SpeechSynthesizer synthesizer ){
- LogDebug ("continue reading ");
- }
- @ Override
- Public void onSpeechProgressChanged (SpeechSynthesizer synthesizer, int progress ){
- }
- @ Override
- Public void onSpeechPause (SpeechSynthesizer synthesizer ){
- LogDebug ("reading suspended ");
- }
- @ Override
- Public void onSpeechFinish (SpeechSynthesizer synthesizer ){
- LogDebug ("reading stopped ");
- }
- @ Override
- Public void onNewDataArrive (SpeechSynthesizer synthesizer, byte [] dataBuffer, int dataLength ){
- LogDebug ("new audio data:" + dataLength );
- }
- @ Override
- Public void onError (SpeechSynthesizer synthesizer, SpeechError error ){
- LogError ("error:" + error. errorDescription + "(" + error. errorCode + ")");
- }
- @ Override
- Public void onCancel (SpeechSynthesizer synthesizer ){
- LogDebug ("canceled ");
- }
- @ Override
- Public void onBufferProgressChanged (SpeechSynthesizer synthesizer, int progress ){
- }
- }