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'1.2 Add Application-Related Permissions
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
| Name | Type | Description | Return Parameter |
|---|---|---|---|
| onStart | Function | Callback method when the engine connection starts | String type: the id of the current task |
| onWarning | Function | Callback method for engine result warnings | Task id and Errors type status code |
| onError | Function | Callback method for engine result errors | Task id and Errors type status code |
| onPlayStart | Function | Callback method where the engine returns the start of playback | None |
| onPlayFinish | Function | Callback method for the engine to return the synthesis end | None |
| onGetAudio | Function | Callback method for the engine to return the synthesized audio data | byte[] type audio data |
| onSynthesisTimestamp | Function | Callback method for engine returns timestamp and phoneme | JsonObject |
| onStop | Function | Callback method for engine returns playback | None |
| (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
| Parameter | Type | Required | Description | Default Value |
|---|---|---|---|---|
| text | String | Yes | Text to be synthesized, length limit: 1024 bytes (UTF-8 encoding) | None |
| lang_type | String | Yes | Language options | None |
| voice | String | No | Voice code | Japanese:Xiaohui English:Julie Chinses:Xiaohui |
| sample_rate | Integer | No | Audio sampling rate, options: 8000, 16000, 24000 | 24000 |
| format | String | No | Audio encoding format,wav / pcm / mp3, Note: wav does not support streaming | pcm |
| compression_rate | Integer | No | Encoding compression ratio for opus format, parameter range [1, 20] | 1 |
| speech_rate | Float | No | Speech rate, parameter range [0.2,3], usually one decimal place is sufficient | 1 |
| volume | Float | No | Volume, parameter range [0.1, 3], usually one decimal place is sufficient | 1 |
| pitch_rate | Float | No | Pitch 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 higher | 0 |
| emotion | String | No | Emotion/style | None |
| silence_duration | Integer | No | Silence duration at the end of a sentence, in milliseconds | 125 |
| enable_timestamp | Boolean | No | Timestamp 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 timestamp | false |
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();