[Android speech synthesis TTS] explanation of Baidu Speech Access Method and usage skills, androidtts
Please respect others' labor achievements and repost the Source: [Android speech synthesis TTS] Baidu Speech Access Method and usage tips
Ps.
Relying on Baidu open cloud, Baidu speech provides industry-leading, permanently free voice technology services to partners. Currently, the services available include speech recognition, semantic parsing, and speech synthesis, in the future, we will continue to provide open resources, multi-round dialogs, and other technical services. Through sdks, REST APIs, and offline Development kits, we will meet the development needs of different developers.
Currently, Baidu speech provides free voice access, and the speech synthesis quality is acceptable, but offline TTS is not yet available.
Service Access Process
For details, see: http://yuyin.baidu.com/dev.php
Integrated Development
Import jar packages and so files related to Baidu speech
Tip:
Copy the libs directory in the Development Kit to the project directory. The libs Directory includes the SO library of each platform. developers can delete the directory as needed. Galaxy_lite.jar is a public basic library of Baidu Android. If other Baidu sdks, such as PushSDK, are integrated in the project, the following error message is displayed during the packaging process:
[11:02:57-Dex Loader] Unableto execute dex: Multiple dex files defineLcom/baidu/android/common/logging/Configuration; [11:02:57-VoiceRecognitionDemo] Conversion to Dalvik format failed: unable to executedex: Multiple dex files define Lcom/baidu/android/common/logging/Configuration;
Remove the Jar package. If the Eclipse ADT version plug-in is earlier than 17, you need to manually add the dependent library by adding Project => Properties => Java Build Path => Libraries => AddJAR... 3
For ease of use, I have further encapsulated the Baidu speech synthesis engine and encapsulated it into SpeechUtil for convenient calling:
Package com. jph. tts; import android. content. context; import android. media. audioManager; import android. util. log; import com. baidu. speechsynthesizer. speechSynthesizer; import com. baidu. speechsynthesizer. speechSynthesizerListener; import com. baidu. speechsynthesizer. publicutility. speechError;/*** speech synthesis tool class ** @ author JPH * @ date 1:33:37 */public class SpeechUtil implements SpeechSynthesizerListe Ner {protected static final int UI_LOG_TO_VIEW = 0; private SpeechSynthesizer speechSynthesizer; private Context context; public SpeechUtil (Context activity) {this. context = activity; init ();}/*** initialize merging related components ** @ author JPH * @ date 1:36:53 */private void init () {speechSynthesizer = new SpeechSynthesizer (context, "holder", this); // Replace the two parameters of the setApiKey method with the apiKey and secretKeysp obtained by registering an application in the Baidu Developer Center. EechSynthesizer. setApiKey ("your apiKey", "your secretKey"); speechSynthesizer. setAudioStreamType (AudioManager. STREAM_MUSIC); // activity. setVolumeControlStream (AudioManager. STREAM_MUSIC); setParams ();} /*** start text synthesis and read aloud * @ author JPH * @ date 1:47:05 * @ param content */public void speak (final String content) {new Thread (new Runnable () {@ Override public void run () {// setParams (); int ret = sp EechSynthesizer. speak (content. toString (); if (ret! = 0) {Log. e ("inf", "failed to start synthesizer:" + ret );}}}). start ();}/*** cancel the merging and stop reading * @ author JPH * @ date 2:20:33 */public void cancle () {speechSynthesizer. cancel ();}/*** pause text reading. If the speak (String) method is not called or the Synthesizer Initialization fails, this method will have no effect * @ author JPH * @ date 2015-4-14 2:21:07 */public void pause () {speechSynthesizer. pause ();}/*** continue to read the text. If the speak (String) method is not called or the Synthesizer Initialization fails, this method will have no effect * @ author JPH * @ date: 21: 29 */public void resume () {speechSynthesizer. resume ();}/*** set parameters for the speech synthesizer * @ author JPH * @ date 1:45:11 */private void setParams () {speechSynthesizer. setParam (SpeechSynthesizer. PARAM_SPEAKER, "0"); // speaker. Currently, female voice (0) and male voice (1) speechSynthesizer are supported. setParam (SpeechSynthesizer. PARAM_VOLUME, "9"); // volume. value range: [0, 9]. The larger the value is, the higher the volume is. speechSynthesizer. setParam (SpeechSynthesizer. PARAM_SPEED, "5"); // read speed, value range: [0, 9]. The larger the value, the faster the language speed. setParam (SpeechSynthesizer. PARAM_PITCH, "5"); // tone. value range: [0, 9]. The greater the value, the higher the volume. speechSynthesizer. setParam (SpeechSynthesizer. PARAM_AUDIO_ENCODE, SpeechSynthesizer. AUDIO_ENCODE_AMR); // audio format, supports bv/amr/opus/mp3. For the values, see the constant declaration speechSynthesizer. setParam (SpeechSynthesizer. PARAM_AUDIO_RATE, SpeechSynthesizer. AUDIO_BITRATE_AMR_15K85); // specifies the audio bit rate. For more information about supported bit rates for each audio format, see the subsequent constant declaration.} @ Overridepublic void onStartWo Rking (SpeechSynthesizer synthesizer) {Log. I ("msg", "start to work, please wait for data... ") ;}@ Overridepublic void onSpeechStart (SpeechSynthesizer synthesizer) {Log. I ("msg", "Start reading") ;}@ Overridepublic void onSpeechResume (SpeechSynthesizer synthesizer) {Log. I ("msg", "Continue reading") ;}@ Overridepublic void onSpeechProgressChanged (SpeechSynthesizer synthesizer, int progress) {// TODO Auto-generated method stub} @ Overridepublic void onSpee ChPause (SpeechSynthesizer synthesizer) {Log. I ("msg", "Read paused") ;}@ Overridepublic void onSpeechFinish (SpeechSynthesizer synthesizer) {Log. I ("msg", "Read stopped") ;}@ Overridepublic void onNewDataArrive (SpeechSynthesizer synthesizer, byte [] audioData, boolean isLastData) {Log. I ("msg", "new audio data:" + audioData. length + (isLastData? "(End)": "") ;}@ Overridepublic void onError (SpeechSynthesizer synthesizer, SpeechError error) {Log. I ("msg", "error:" + error. errorDescription + "(" + error. errorCode + ")") ;}@ Overridepublic void onCancel (SpeechSynthesizer synthesizer) {Log. I ("msg", "canceled") ;}@ Overridepublic void onBufferProgressChanged (SpeechSynthesizer synthesizer, int progress) {// TODO Auto-generated method stub }}
Usage:
package com.jph.tts;import android.app.Activity;import android.os.Bundle;import android.text.method.ScrollingMovementMethod;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import com.baidu.tts.sample.R;public class HomeActivity extends Activity implements OnClickListener {protected static final int UI_LOG_TO_VIEW = 0;private TextView logView;private EditText inputTextView;private Button startButton;private SpeechUtil speechUtil;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_home);logView = (TextView) findViewById(R.id.logView);logView.setMovementMethod(new ScrollingMovementMethod());inputTextView = (EditText) findViewById(R.id.inputTextView);speechUtil = new SpeechUtil(this);}@Override public void onClick(View v) { switch (v.getId()) { case R.id.btnStart: speechUtil.speak(inputTextView.getText().toString()); break; case R.id.btnPause: speechUtil.pause(); break; case R.id.btnResume: speechUtil.resume(); break; case R.id.btnStop: speechUtil.cancle(); break; default: break; } }}
Because Baidu TTS requires Internet resolution, you need to grant the corresponding permissions to the application:
<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"/>
Recommended reading:
[Android speech synthesis TTS] comparison of mainstream domestic Engines
[Android speech synthesis TTS] detailed description on offline TTS usage