Using Unisound Realization Android Voice Broadcast

It is relatively easy to implement voice broadcast on Android. The first reaction is to use the TTS that comes with the system. However, the support for Chinese is not good. Secondly, it is inconvenient to customize the voice effect. Third-party voice broadcast solutions are a natural choice.

There are many third-party voice broadcast programs, such as Xunfei, Baidu, and Unisound. This article uses offline broadcast programs. Xunfei’s offline charges are not low. Baidu is the default network, so only Unisound is optional.

Unisound’s vision into the public should be at the launch of the Smartisan. Like every open platform, you must first register as a developer.

Preliminary Preparation

  1. Register as a Unisound developer

Go to Unisound Open platform, Fill in the registration information to become a developer.

  1. Add new app

Click My Apps and select “Add New Application”. We want to use offline voice synthesis, so choose the general solution and fill in the introduction information. Complete the app registration.

  1. Check APPKEY and SECRET

Click on the newly created application, you can see the APPKEY and SECRET corresponding to the application.

  1. Download the Development Kit

Go to the SDK Download Page, select “Universal Solution - Android - Offline Speech Synthesis”, and then download the SDK.

  1. Configuration project structure

Find the Demo and the documentation from the downloaded SDK development package. It should be noted that the Demo is a project built with Eclipse, so the project structure is slightly different, and it can be configured according to the following figure. usc.jar is placed in the libs directory under the app, and the two so files are placed in jniLibs under main.

Application Development

To use the offline voice service, you also need a voice model. This project below puts the voice model in the assets and then copies the code to the SD card. Each project can handle the model storage location according to the scene requirements.

  1. First new a file named TTSUtils.java, as follows
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

/**
* Created by SymeonChen on 2018/2/5.
*/

public class TTSUtils {
public static TTSUtils sInstance = new TTSUtils();

public TTSUtils() {
}

public static TTSUtils getsInstance() {
return sInstance;
}

public SpeechSynthesizer getmTTSPlayer() {
return mTTSPlayer;
}

public void setmTTSPlayer(SpeechSynthesizer mTTSPlayer) {
this.mTTSPlayer = mTTSPlayer;
}

private SpeechSynthesizer mTTSPlayer;

public void initTts(Context context) {

// Init
mTTSPlayer = new SpeechSynthesizer(context, APPKEY, SECRET);
// Set up local composition
mTTSPlayer.setOption(SpeechConstants.TTS_SERVICE_MODE, SpeechConstants.TTS_SERVICE_MODE_LOCAL);

// Set up fortend model
mTTSPlayer.setOption(SpeechConstants.TTS_KEY_FRONTEND_MODEL_PATH, mFrontendModel);
// Set up backend model
mTTSPlayer.setOption(SpeechConstants.TTS_KEY_BACKEND_MODEL_PATH, mBackendModel);
// Set up listener
mTTSPlayer.setTTSListener(new SpeechSynthesizerListener() {

@Override
public void onEvent(int type) {
switch (type) {
case SpeechConstants.TTS_EVENT_INIT:
// Callback of init success
Log.i("TTSUtils", "onInitFinish");
break;
case SpeechConstants.TTS_EVENT_SYNTHESIZER_START:
// Callback of synthesis start
Log.i("TTSUtils", "beginSynthesizer");
break;
case SpeechConstants.TTS_EVENT_SYNTHESIZER_END:
// Callback of synthesis end
Log.i("TTSUtils", "endSynthesizer");
break;
case SpeechConstants.TTS_EVENT_BUFFER_BEGIN:
// Callback of caching start
Log.i("TTSUtils", "beginBuffer");
break;
case SpeechConstants.TTS_EVENT_BUFFER_READY:
// Callback of caching end
Log.i("TTSUtils", "bufferReady");
break;
case SpeechConstants.TTS_EVENT_PLAYING_START:
// Callback of play begin
Log.i("TTSUtils", "onPlayBegin");
break;
case SpeechConstants.TTS_EVENT_PLAYING_END:
// Callback of play end
Log.i("TTSUtils", "onPlayEnd");
break;
case SpeechConstants.TTS_EVENT_PAUSE:
// Callback of play pause
Log.i("TTSUtils", "pause");
break;
case SpeechConstants.TTS_EVENT_RESUME:
// Callback of play resume
Log.i("TTSUtils", "resume");
break;
case SpeechConstants.TTS_EVENT_STOP:
// Callback of play stop
Log.i("TTSUtils", "stop");
break;
case SpeechConstants.TTS_EVENT_RELEASE:
// Callback of resouce release
Log.i("TTSUtils", "release");
break;
default:
break;
}

}

@Override
public void onError(int type, String errorMSG) {
// handle error of processing


}
});
// Initialize the composition engine
mTTSPlayer.init("");
}
}
  1. Initialize at the appropriate location

Initialize in Application.class or specific Activity

1
TTSUtils.getsInstance().initTts(this);

Then use the following code to play.

1
TTSUtils.getsInstance().getmTTSPlayer().playText("Put your text here to play(Include Chinese)");

Attention

  1. You need to add jar related in the build.gradle dependencies
1
implementation files('libs/usc.jar')
  1. If the voice model is placed in the assets, it takes time (about 1 second) to copy from the assets to the sd card. If there is no model when the cloud is initialized, an empty exception will be thrown when the playback function is called.
  2. There are fewer offline voice models, and more models can be contacted for business purchases or online.
  3. The development documentation is old and there is no technical support. It is recommended to understand the function through the Demo code.