Android Studio Application Development integrated Baidu speech synthesis Use Method Example explains _android

Source: Internet
Author: User
Tags auth gettext int size

First, speech synthesis refers to the conversion of text information into sound. The idea is to turn the text into a sound and let your application speak. Domestic in the industry more famous Third-party speech synthesis platform has Baidu voice and Hkust news fly.

The integration of this article is the Baidu speech synthesis, its main features are:

Totally permanent free

The industry pioneered a completely permanent free new form for developers to provide the most fluent and natural voice synthesis services. Completely free, permanently used, completely out of limits.

Offline Online Fusion mode

SDK can be based on the current network situation, automatically judge the use of local engine or cloud engine for speech synthesis, no need to worry about traffic consumption!

Multi-lingual multi-tone optional

Mandarin, Chinese and English mixed reading, male, female, you choose, more support speed, tone, volume, audio rate settings, so that your application has the most sweet and the most magnetic voice!

Smooth and natural synthesis effect

Speech synthesis technology industry-leading, synthetic effect close to real people voice, smooth and natural, and very expressive, give you the most comfortable hearing experience!

Baidu Voice of the official demo is written in the eclipse environment, and in the Android studio is a little different, below please see Baidu Speech synthesis using detailed steps (step by step do not jump impatient to eat hot tofu):

1, registered Baidu voice developer platform

Registered Baidu account, registered developer information, create applications, you can get APP ID, API key, and Secret key, in the development process will use these three values to authorize (very key), open speech synthesis service, if need to use off-line synthesis function also need to apply for offline authorization. Detailed steps please see Baidu Voice Access process.

Key value view (critical)

2. Download Resources

Baidu Voice SDK Download, according to their own needs to download, this sample download is from the online fusion speech synthesis sdk_android version, address: Http://yuyin.baidu.com/tts/download

3, Integrated Baidu Voice Guide

3.1 Add jar packs and so libraries to engineering

The Libs directory in the development package is copied to the engineering directory as a whole (users of Eclipse), and the Libs directory includes the jar package and the so library of each platform, and the developer can cut the application needs. Galaxy_lite.jar is Baidu's Android public base library, if the project also integrates other Baidu SDK,
such as the Push SDK, an error message similar to the following appears in the packaging process:

[2013-10-22 11:02:57-dex Loader] Unable to execute Dex:multiple Dex files define
Lcom/baidu/android/common/logging/configuration;
[2013-10-22 11:02:57-voicerecognitiondemo] Conversion to Dalvik format failed:unable to
Execute dex:multiple Dex files define lcom/baidu/android/common/logging/configuration;

Please remove this Jar package. For users who use Android studio, you should put the jar packages in the Libs directory under the Libs directory, and then add library dependencies (don't forget). So library should be placed in the Jnilibs directory, jnilibs directory and Java, res in the same directory. Create your own if you don't have a corresponding directory. The entire structure is shown below:

3.2 Adding speech synthesis resource files

Place the DAT files in the data directory in the development package under the assets directory of the project, where the assets directory is in the same directory as Java and res in order to set the resource file parameters.

3.3 Permission Statement

Use Baidu Voice needs to declare the following permissions:

<uses-permission android:name= "Android.permission.INTERNET"/>
<uses-permission android:name= " Android.permission.ACCESS_NETWORK_STATE "/>
<uses-permission android:name=" android.permission.MODIFY_ Audio_settings "/>
<uses-permission android:name=" Android.permission.WRITE_EXTERNAL_STORAGE "/>"
<uses-permission android:name= "Android.permission.WRITE_SETTINGS"/>
<uses-permission android:name= " Android.permission.READ_PHONE_STATE "/>
<uses-permission android:name=" android.permission.ACCESS_WIFI_ State "/>
<uses-permission android:name=" Android.permission.CHANGE_WIFI_STATE "/>

4, Speech synthesis function code

4, 1 TTS initialization

Get TTS Instance SpeechSynthesizer = Speechsynthesizer.getinstance (); 
Set the app context (required parameters) Speechsynthesizer.setcontext (contexts);
Set up TTS listener Speechsynthesizer.setspeechsynthesizerlistener (Speechsynthesizerlistener); Text model file path, file absolute path (Offline engine usage) speechsynthesizer.setparam (speechsynthesizer.param_tts_text_model_file, Text_model_ 
File_full_path_name); Acoustic model file path, file absolute path (Offline engine usage) speechsynthesizer.setparam (speechsynthesizer.param_tts_speech_model_file, Speech_ 
Model_file_full_path_name); The local authorization file path, if not set, will use the default path. Set the temporary authorization file path,//licence_file_name replace the actual path to the temporary authorization file, only need to set when using temporary license file,///If offline authorization is opened in [Application management], it is recommended that the line code be deleted 
(Off-line engine) Speechsynthesizer.setparam (Speechsynthesizer.param_tts_licence_file, license_file_full_path_name); 
Replace the app ID (offline authorization) speechsynthesizer.setappid ("your_app_id") registered with the application on the Voice developer platform; 
Replace the Apikey and Secretkey (online authorization) Speechsynthesizer.setapikey ("Your_api_key", "Your_secret_key") for the Voice developer Platform registration application; Authorization Detection interface AuthInfo AuthInfo = SpeechsYnthesizer.auth (Ttsmode); Engine initialization Interface Speechsynthesizer.inittts (Ttsmode);

Note: Copy the resource files from the Assets folder to the SD card before initializing the settings for use. In addition, offline authorization temporary files valid for only 30 days, to the long-term use of voice off-line synthesis should be in the application management to open offline authorization.

4, 2 synthesis and playback

Mspeechsynthesizer.speak (text);

This interface is time-consuming, adopting queuing strategy, which will automatically join the synthesis queue and be synthesized and played in the Order of invocation.

OK, so your speech synthesis can be used, if you want to make more parameter settings, please see Baidu Speech synthesis official development document and Development manual.

5, source code

Finally affixed to my source code.

Manifest file: Androidmanifest.xml

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= "http://schemas.android.com/apk/res/"
Android "package=" Com.junkchen.bdttsdemo > <uses-permission android:name= "Android.permission.INTERNET"/> <uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name= "Android.permission.MODIFY_AUDIO_SETTINGS"/> <uses-permission android:name= "android.permission.WRITE_ External_storage "/> <uses-permission android:name= Android.permission.WRITE_SETTINGS"/> < Uses-permission android:name= "Android.permission.READ_PHONE_STATE"/> <uses-permission android:name= " Android.permission.ACCESS_WIFI_STATE "/> <uses-permission android:name=" android.permission.CHANGE_WIFI_ State "/> <application android:allowbackup=" true "android:icon=" @mipmap/ic_launcher "android:label=" @string/ App_name "android:supportsrtl=" true "android:theme=" @style/apptheme "> <activity android:name=". Mainactivity "> &Lt;intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category " Android.intent.category.LAUNCHER "/> </intent-filter> </activity> </application> </manifest >

Layout layout file: Activity_main.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
xmlns:tools=" Http://schemas.android.com/tools "
android:layout_width=" Match_parent
" android:layout_height= "Match_parent"
android:orientation= "vertical"
android:padding= "16DP"
tools: context= "Com.junkchen.bdttsdemo.MainActivity" >
<edittext
android:id= "@+id/edt_content"
Android:layout_width= "Match_parent"
android:layout_height= "wrap_content"
android:gravity= "Start"
android:minlines= "5"
android:text= "Hi, I am Baidu speech synthesis, please enter the speech content to be synthesized"/>
<button
android:id= "@+ Id/btn_speak "
android:layout_width=" match_parent "
android:layout_height=" Wrap_content "
android: Text= "speech synthesis and playback"/>
</LinearLayout>

Java:MainActivity.java

Package Com.junkchen.bdttsdemo;
Import Android.os.Bundle;
Import android.os.Environment;
Import android.support.v7.app.AppCompatActivity;
Import Android.util.Log;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Com.baidu.tts.answer.auth.AuthInfo;
Import Com.baidu.tts.client.SpeechError;
Import Com.baidu.tts.client.SpeechSynthesizer;
Import Com.baidu.tts.client.SpeechSynthesizerListener;
Import Com.baidu.tts.client.TtsMode;
Import Java.io.File;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;  public class Mainactivity extends Appcompatactivity implements Speechsynthesizerlistener {private static final String TAG
= "Mainactivity";
Private SpeechSynthesizer mspeechsynthesizer;//Baidu voice synthesis client private String Msampledirpath;
private static final String Sample_dir_name = "Baidutts";
private static final String Speech_female_model_name = "Bd_etts_speech_female.dat"; PrivatE static final String speech_male_model_name = "Bd_etts_speech_male.dat";
private static final String Text_model_name = "Bd_etts_text.dat";
private static final String License_file_name = "temp_license_2016-04-05";
private static final String English_speech_female_model_name = "Bd_etts_speech_female_en.dat";
private static final String English_speech_male_model_name = "Bd_etts_speech_male_en.dat";
private static final String English_text_model_name = "Bd_etts_text_en.dat"; private static final String app_id = "7957876";//Please replace the application private static final String api_key = "Cvn31pilxbhrndgdlnhyeu" that you created for yourself
Yq ";//Please replace the application private static final String Secret_key =" 84e6987b56f11e6ee97e02ef25a2b4f0 "that you created for yourself;//Please replace the application you created for yourself @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.activity_main);
Initialenv ();
Initialtts ();
Initview (); @Override protected void OnDestroy () {this.mSpeechSynthesizer.release ();//Free resources super.ondestroy (); private EdIttext edt_content;
Private Button Btn_speak; private void Initview () {edt_content = (edittext) Findviewbyid (r.id.edt_content); btn_speak = (Button) Findviewbyid (R.id
. Btn_speak); Btn_speak.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {String content = Edt_con
Tent.gettext (). toString ();
Mspeechsynthesizer.speak (content);
LOG.I (TAG, ">>>say:" + edt_content.gettext (). toString ());}); /** * Initialize speech synthesis client and start/private void Initialtts () {//Get speech Composite object instance This.mspeechsynthesizer = Speechsynthesizer.getinstance ()
;
Set the context this.mSpeechSynthesizer.setContext (this);
Set up speech synthesis status monitoring This.mSpeechSynthesizer.setSpeechSynthesizerListener (this); Text Model file path (offline engine usage) this.mSpeechSynthesizer.setParam (speechsynthesizer.param_tts_text_model_file, Msampledirpath +
"/" + text_model_name); Acoustic model file path (offline engine usage) This.mSpeechSynthesizer.setParam (speechsynthesizer.param_tts_speech_model_file,
Msampledirpath + "/" + speech_female_model_name); Local authorization file path, if not set will use default roadPath. Set temporary authorization file paths, Licence_file_name please replace the actual path of the temporary authorization file//only when using temporary license files, if offline authorization is opened in [Application management],//You do not need to set this parameter, It is recommended that this line of code be deleted (offline engine) This.mSpeechSynthesizer.setParam (speechsynthesizer.param_tts_licence_file, Msampledirpath + "/" +
License_file_name);
Replace the app ID (offline authorization) this.mSpeechSynthesizer.setAppId (APP_ID) registered with the application on the Voice developer platform;
Replace the Apikey and Secretkey (online authorization) This.mSpeechSynthesizer.setApiKey (Api_key, Secret_key) registered with the Voice developer platform for Applications;
Pronunciation person (online engine), available parameters for 0,1,2,3 ... (The server side will be dynamically incremented, each value meaning reference documentation, whichever is the document description.) 0--Ordinary female, 1--ordinary male, 2--special male, 3--emotional male voice ...
) This.mSpeechSynthesizer.setParam (Speechsynthesizer.param_speaker, "0"); Set up a MIX-mode synthesis Strategy This.mSpeechSynthesizer.setParam (Speechsynthesizer.param_mix_mode, Speechsynthesizer.mix_mode_
DEFAULT);
Authorization detection interface (can not be used, just to verify that authorization is successful) AuthInfo AuthInfo = This.mSpeechSynthesizer.auth (Ttsmode.mix); if (authinfo.issuccess ()) {log.i (TAG, ">>>auth success.");} else {String errormsg = Authinfo.getttserror (). Get
Detailmessage ();
LOG.I (TAG, ">>>auth failed ErrorMsg:" + errormsg);} Engine InitialMspeechsynthesizer.inittts (Ttsmode.mix) of TTS interface; Load Offline English resources (provide offline English synthesis function) int result = Mspeechsynthesizer.loadenglishmodel (Msampledirpath + "/" + English_text_model_name
, Msampledirpath + "/" + english_speech_female_model_name);
LOG.I (TAG, ">>>loadenglishmodel result:" + result);} @Override public void Onsynthesizestart (String s) {//Supervisor hearing synthesis start log.i (TAG, ">>>onsynthesizestart () <<< s
: "+ s);} @Override public void onsynthesizedataarrived (String s, byte[] bytes, int i) {//supervisor hears of synthetic data arriving log.i (TAG, ">>>onsynt
Hesizedataarrived () <<< S: "+ s);} @Override public void Onsynthesizefinish (String s) {//Supervisor hears synthetic end log.i (TAG, ">>>onsynthesizefinish () <<
< s: "+ s);} @Override public void Onspeechstart (String s) {//Supervisor hears compositing and starts playing log.i (TAG, ">>>onspeechstart () <<< S:" + s)
; @Override public void onspeechprogresschanged (String s, int i) {//Supervisor hears playback progress changes log.i (TAG, "&GT;&GT;&GT;ONSPEECHPROGRESSC Hanged () <<< s: "+ s);}
@Override public void Onspeechfinish (String s) {//Supervisor hears playback end log.i (TAG, ">>>onspeechfinish () <<< S:" + s)
; @Override public void OnError (String s, Speecherror speecherror) {//Supervisor heard error log.i (TAG, ">>>onerror () <<&lt ;
Description: "+ speecherror.description +", Code: "+ Speecherror.code";} private void Initialenv () {if (Msampledirpath = = null) {String Sdcardpath = Environment.getexternalstoragedirectory (). To
String ();
Msampledirpath = Sdcardpath + "/" + sample_dir_name;
File File = new file (Msampledirpath); if (!file.exists ()) {file.mkdirs ();} Copyfromassetstosdcard (False, Speech_female_model_name, Msampledirpath + "/" + SPE
Ech_female_model_name);
Copyfromassetstosdcard (False, Speech_male_model_name, Msampledirpath + "/" + speech_male_model_name);
Copyfromassetstosdcard (False, Text_model_name, Msampledirpath + "/" + text_model_name);
Copyfromassetstosdcard (False, License_file_name, Msampledirpath + "/" + license_file_name); Copyfromassetstosdcard (fAlse, "english/" + english_speech_female_model_name, Msampledirpath + "/" + english_speech_female_model_name); Copyfromassetstosdcard (False, "english/" + english_speech_male_model_name, Msampledirpath + "/" + English_speech_male_
Model_name);
Copyfromassetstosdcard (False, "english/" + english_text_model_name, Msampledirpath + "/" + english_text_model_name); /** * Copy the required resource files to the SD card (the authorization file is a temporary authorization file, please register the official authorization) * * @param iscover overwrite the existing target file * @param source * @param dest/Public V OID Copyfromassetstosdcard (Boolean iscover, String source, String dest) {File File = new file (dest); if (Iscover | | (!iscover &&!file.exists ())
{InputStream is = null;
FileOutputStream fos = null;
try {is = Getresources (). Getassets (). open (source);
String path = dest;
FOS = new FileOutputStream (path);
byte[] buffer = new byte[1024];
int size = 0; while (size = is.read (buffer, 0, 1024)) >= 0) {fos.write (buffer, 0, size);}} catch (FileNotFoundException e) {e.printstacktrace ();} catch (Ioexception e) {e.printstacktrace ();} finally {if (FOS!= null) {try {fos.close ();} catch (IOException e) {e.printstacktrace (
);
The try {if (is!= null) {Is.close ()}} catch (IOException e) {e.printstacktrace ();}} }
}
}

The above is a small part of the introduction of Android Studio application development integrated Baidu Speech synthesis use of examples to explain, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.