Logo
Short Text to Speech

Android SDK

TTS Android SDK

1 Integration Steps

1.1 Add aar Dependency

Place the android-tts-sdk-release-V1.0.0.0.aar file into the libs directory of your project, and modify the build.gradle file of the app module to add the aar file and okhttp as dependencies.

implementation fileTree(dir:"libs",include:["*.jar","*.aar"])
implementation'com.squareup.okhttp3:okhttp:4.9.0'

Modify the AndroidManifest.xml file

<!--Internet Permission-->
<uses-permission android:name="android.permission.INTERNET" />

2. Call Steps/Example Code

2.1 Get Instance

⑴ Create Synthesis Class

     Tts tts=Tts.getInstance(this,((code,msg)->{
        if("00000".equals(code)){
        System.out.println("Success");
        }else{
        Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
        }
      }));  

⑵ Set Callback

(1)Callback Parameter Description

NameTypeDescriptionReturn Parameter
onStartFunctionCallback method when the engine connection startsString type: the id of the current task
onWarningFunctionCallback method for engine result warningsTask id and Errors type status code
onErrorFunctionCallback method for engine result errorsTask id and Errors type status code
onPlayStartFunctionCallback method where the engine returns the start of playbackNone
onPlayFinishFunctionCallback method for the engine to return the synthesis endNone
onGetAudioFunctionCallback method for the engine to return the synthesized audio databyte[] type audio data
onSynthesisTimestampFunctionCallback method for engine returns timestamp and phonemeJsonObject
onStopFunctionCallback method for engine returns playbackNone
(2)Parameter Example
tts.setListener(new Tts.Listener(){
@Override
public void onStart(String taskId){}

@Override
public void onStop(){}

@Override
public void onError(String taskId,ErrorCodes.ErrorCode err){}

@Override
public void onGetAudio(byte[]data){}

@Override
public void onPlayStart(){}

@Override
public void onPlayFinish(){}

@Override
public void onSynthesisTimestamp(JsonObject payload){}

@Override
public void onWarning(String taskId,ErrorCodes.ErrorCode err){}

        });

3. Parameter Settings

ParameterTypeRequiredDescriptionDefault Value
textStringYesText to be synthesized, length limit: 1024 bytes (UTF-8 encoding)None
lang_typeStringYesLanguage optionsNone
voiceStringNoVoice codeJapanese:Xiaohui
English:Julie
Chinses:Xiaohui
sample_rateIntegerNoAudio sampling rate, options: 8000, 16000, 2400024000
formatStringNoAudio encoding format,wav / pcm / mp3,
Note: wav does not support streaming
pcm
compression_rateIntegerNoEncoding compression ratio for opus format, parameter range [1, 20]1
speech_rateFloatNoSpeech rate, parameter range [0.2,3], usually one decimal place is sufficient1
volumeFloatNoVolume, parameter range [0.1, 3], usually one decimal place is sufficient1
pitch_rateFloatNoPitch rate, represented by a number within the range [-1.0, 1.0] in octaves, -1.0 means one octave lower, 1.0 means one octave higher0
emotionStringNoEmotion/styleNone
silence_durationIntegerNoSilence duration at the end of a sentence, in milliseconds125
enable_timestampBooleanNoTimestamp related, passing true enables it, and it can return the timestamp of the original text, rather than the text after TN, it retains the Arabic numerals or special symbols in the original text. Note: Multiple punctuation marks or spaces in the original text will still be processed, but it does not affect the continuity of the timestampfalse
   Tts.Params params=tts.getParams();
        params.setText(text);
        params.setSampleRate(24000);
        params.setFormat("pcm");
        params.setLang("zh-cmn-Hans-CN");

4. Start/Stop Synthesis

tts.start();
tts.stop(); 

5. SDK download

Android SDK

Android Demo