20.5 speech Synthesis (Baidu February 29, 2016 to release the TTS engine) _android

Source: Internet
Author: User
Tags auth

First, Introduction

When writing mobile app, sometimes it is necessary to use text-to-speech (text to Speech) function, such as reading received SMS when driving, navigation voice prompts, the important information in the interface through voice emphasis 、...... Wait

Since the Android Pico TTS does not support Chinese, it is necessary to download Chinese text and read English text, and the Chinese-speaking voice packs provided by a third party must be downloaded.

Second, apply for Baidu TTS authorization

This section of Baidu February 29, 2016 released "from the online fusion speech synthesis sdk_android 2.2.3 version" as an example of using C # to achieve the basic use of speech synthesis. The reason for the choice of Baidu speech synthesis to achieve, because according to Baidu official statement, the development package is "permanent free". The Web site is as follows:

http://yuyin.baidu.com/tts/

Since you have already applied for Mydemos authorization, it is simpler to continue applying for the TTS license, and the application and setup steps are as follows.

1. Application for authorization

Enter Http://yuyin.baidu.com/tts/'s homepage:

Click "Use Now" to enter the "Open Speech Synthesis Service" page:


In the dropdown box, select an application that you have already requested, click Next, and then follow the prompts to open the offline service.

2. Transform jar file in Bdmapv371bindinglib project

First, download the corresponding development package through the "related downloads" in the http://yuyin.baidu.com/tts/home page, and then follow the steps below.

1. Add the Com.baidu.tts_2.2.3.20160229_359d952_release.jar, Galaxy-v2.0.jar in the example to the Jars folder, as shown in the following illustration, and set its Build Action property to " Embeddedjar ".

2, add the following statement in the Metadata.xml file:

 
 

3, rebuild the project, there should be no errors at this time.

After these 3 steps, the process of importing and converting a TTS jar package into a. cs file is completed.

3, add the. So file in the Mydemos project

Add TTS-related 4. So files to the Mydemos project's x86 folder, as shown in the following illustration, and then set its Build Action property to "Androidnativelibrary".

4. Add the. dat files to the Baidutts folder of the SD card

Add a way to see "Frequently asked Questions", no more screenshots here.

You can also add these files to the assets folder and then copy them to the SD card folder by code. For simplicity, this is done directly through manual copying.

OK, after these 4 steps, you can easily use Baidu TTS to realize the function of speech reading in any module of Mydemos project.

Third, the example

1, run the screenshot

When you click Read, you will automatically read the contents of the text box with a female accent, and when you click "Bulk reading", you will read aloud the text segments added to the queue (mainly to demonstrate reading a variety of different Chinese and English phrases).

2. Design steps

(1) Add Ch2005main.axml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical " > <linearlayout android:layout_width= "fill_parent" android:layout_height= "50DP" android:orientation= " Horizontal "android:weightsum=" 4 "> <button android:id=" @+id/speak "android:layout_width=" Fill_parent "Android : layout_height= "Fill_parent" android:layout_weight= "1" android:lines= "2" android:text= "reading" android:textsize= "12DP"
/> <button android:id= "@+id/pause" android:layout_width= "fill_parent" android:layout_height= "Fill_parent" android:layout_weight= "1" android:lines= "2" android:text= "suspend" android:textsize= "12DP"/> <button android:id= "@
+id/resume "android:layout_width=" fill_parent "android:layout_height=" fill_parent "android:layout_weight=" "1" Android:lines= "2" android:text= "Continue" android:textsize= "12DP"/> <button android:id= "@+id/stop" aNdroid:layout_width= "Fill_parent" android:layout_height= "Fill_parent" android:layout_weight= "1" android:lines= "2" android:text= "Stop" android:textsize= "12DP"/> </LinearLayout> <linearlayout android:layout_width= "Fill_" Parent "android:layout_height=" 50DP "android:orientation=" Horizontal "android:weightsum=" 4 "> <button android: Id= "@+id/synthesize" android:layout_width= "fill_parent" android:layout_height= "Fill_parent" android:layout_weight
= "1" android:lines= "2" android:text= "synthesize" android:textsize= "12DP"/> <button android:id= "@+id/play" Android:layout_width= "Fill_parent" android:layout_height= "Fill_parent" android:layout_weight= "1" android:lines= "2 "android:text=" Play "android:textsize=" 12DP "/> <button android:id=" @+id/batchspeak "android:layout_width="
Fill_parent "android:layout_height=" Fill_parent "android:layout_weight=" 1 "android:lines=" 2 "android:text=" bulk reading " Android:textsize= "12DP"/> <button android:id= "@+id/nextactivity" Android:layout_widtH= "Fill_parent" android:layout_height= "Fill_parent" android:layout_weight= "1" android:lines= "2" android:enabled= "
False "android:text=" android:textsize= "12DP"/> </LinearLayout> <edittext android:id= "@+id/input"
Android:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:hint= "input"/> <TextView Android:id= "@+id/showtext" android:layout_width= "fill_parent" android:layout_height= "Fill_parent" Android:layout_ Margin= "10DP" android:background= "@android: Color/darker_gray" android:minlines= "3" android:scrollbars= "vertical"/ > </LinearLayout>

2, adding Ch2005MainActivity.cs

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using Android.app;
Using Android.os;
Using Android.widget;
Using Com.Baidu.Tts.Client;
Using Com.Baidu.Tts.Answer.Auth; namespace Mydemos.srcdemos {[Activity (Label = "" Example 20-5 "Baidu TTS basic Usage")] public class ch2005mainactivity:activity, Ispeechsy Nthesizerlistener {private edittext minput; private TextView mshowtext; private SpeechSynthesizer Mspeechsynthesizer;
/<summary>///SD card to save the path of Baidu TTS file///</summary> private string Msampledirpath;
Private Const string speechfemalemodelname = "Bd_etts_speech_female.dat";
Private Const string speechmalemodelname = "Bd_etts_speech_male.dat";
Private Const string textmodelname = "Bd_etts_text.dat";
Private Const string englishspeechfemalemodelname = "Bd_etts_speech_female_en.dat";
Private Const string englishspeechmalemodelname = "Bd_etts_speech_male_en.dat";
Private Const string englishtextmodelname = "Bd_etts_text_en.dat"; protected override void ONCREate (Bundle savedinstancestate) {base.
OnCreate (savedinstancestate);
Setcontentview (Resource.Layout.ch2005Main);
Msampledirpath = Android.OS.Environment.ExternalStorageDirectory.Path + "/baidutts";
Console.WriteLine ("Msampledirpath=" + Msampledirpath);
InitialView ();
Initialtts ();
private void Initialtts () {mspeechsynthesizer = Speechsynthesizer.instance; Mspeechsynthesizer.setcontext (this);
Mspeechsynthesizer.setspeechsynthesizerlistener (this); Text Model file path (offline engine usage) mspeechsynthesizer.setparam (speechsynthesizer.paramttstextmodelfile, Msampledirpath + "/" +
Textmodelname); Acoustic model file path (offline engine usage) mspeechsynthesizer.setparam (speechsynthesizer.paramttsspeechmodelfile, Msampledirpath + "/" +
Speechfemalemodelname);
Replace the app ID (offline authorization)//mspeechsynthesizer.setappid ("your_app_id") registered with the application on the Voice developer platform; Mspeechsynthesizer.setappid (Ch.
TTSAPPID); Please replace the Apikey and Secretkey (online licensing)//this.mspeechsynthesizer.setapikey ("Your_api_key", "Your_secret_key") that are registered with the Voice developer platform.
); This.mSpeechSynthesizer.SetApiKey (Ch. TTsapikey, ch.
Ttssecretkey); 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 ...
) Mspeechsynthesizer.setparam (Speechsynthesizer.paramspeaker, "0");
Set up mix-mode synthesis Strategy Mspeechsynthesizer.setparam (Speechsynthesizer.parammixmode, Speechsynthesizer.mixmodedefault);
Authorization detection interface (can not be used, just to verify that authorization is successful) AuthInfo AuthInfo = This.mSpeechSynthesizer.Auth (Ttsmode.mix); if (authinfo.issuccess) {Console.WriteLine ("Authorization detection--Authorization is successful" (Auth success).
");
}
else {string errormsg = AuthInfo.TtsError.DetailMessage;
Console.WriteLine ("Authorization detection--Authorization failure (auth failed), errormsg=" + errormsg);
//Initialization of TTS Mspeechsynthesizer.inittts (Ttsmode.mix); Load Offline English resources (provide offline English synthesis function) int result = Mspeechsynthesizer.loadenglishmodel (Msampledirpath + "/" + Englishtextmodelname, MS
Ampledirpath + "/" + englishspeechfemalemodelname); private void InitialView () {minput = findviewbyid<edittext> (Resource.Id.input); Minput.text = "Sunny Today, sunny day!"
";
Mshowtext = findviewbyid<textview> (Resource.Id.showText); VaR speak = findviewbyid<button> (Resource.Id.speak); Speak. 
Click + = Delegate {string text = This.mInput.Text int result = This.mSpeechSynthesizer.Speak (text); if (Result < 0) { System.Diagnostics.Debug.WriteLine ("error code: {0}", please check the meaning of the corresponding error code in Baidu TTS document.)
", result);};
var pause = findviewbyid<button> (Resource.Id.pause); Pause.
Click + + Delegate {mspeechsynthesizer.pause ();};
var resume = findviewbyid<button> (Resource.Id.resume); Resume.
Click + + Delegate {mspeechsynthesizer.resume ();};
var stop = findviewbyid<button> (Resource.Id.stop); Stop.
Click + + Delegate {mspeechsynthesizer.stop ();};
var synthesize = findviewbyid<button> (Resource.Id.synthesize); Synthesize.  Click + = Delegate {string text = This.mInput.Text int result = this.mSpeechSynthesizer.Synthesize (text); if (Result < 0) {System.Diagnostics.Debug.WriteLine ("Error,please Look up error code in doc" or URL:HTTP://YUYIN.BAIDU.COM/DOCS/TTS/1
22 ");}; var play = findviewbyid<button> (Resource.
Id.play); Play.
Click + = delegate {};
var batchspeak = findviewbyid<button> (Resource.Id.batchSpeak); Batchspeak.click + = Delegate {list<speechsynthesizebag> bags = new list<speechsynthesizebag> (); bags.
ADD (Getspeechsynthesizebag ("123456", "0")); Bags.
ADD (Getspeechsynthesizebag ("Hello", "1")); Bags.
ADD (Getspeechsynthesizebag ("Use Baidu speech synthesis SDK", "2")); Bags.
ADD (Getspeechsynthesizebag ("Hello", "3")); Bags.
ADD (Getspeechsynthesizebag ("This is a demo project", "4"));
int result = This.mSpeechSynthesizer.BatchSpeak (bags); if (Result < 0) {System.Diagnostics.Debug.WriteLine ("error ({0})], please look up the error code in DOC or url:http://yuyin.b
aidu.com/docs/tts/122 ", result);}};} protected override void OnDestroy () {base.
OnDestroy (); Private Speechsynthesizebag Getspeechsynthesizebag (string text, String Utteranceid) {Speechsynthesizebag
Speechsynthesizebag = new Speechsynthesizebag ();
Speechsynthesizebag.settext (text);
Speechsynthesizebag.utteranceid = Utteranceid; Return SpeechsynthesiZebag; } public void OnError (string utteranceid, speecherror error) {Console.WriteLine ("OnError error=" + error.)
Description + "--utteranceid=" + Utteranceid); } public void Onspeechfinish (string Utteranceid) {Console.WriteLine ("Onspeechfinish utteranceid=" + Utteranceid);} Publ IC void onspeechprogresschanged (string p0, int p1) {//console.writeline ("onspeechprogresschanged");} public void Onspee Chstart (String Utteranceid) {Console.WriteLine ("Onspeechstart utteranceid=" + Utteranceid);} public void Onsynthesizedataarrived (String Utteranceid, byte[] data, int progress) {Console.WriteLine ("onsynthesizedataarrived")
;
} public void Onsynthesizefinish (string Utteranceid) {Console.WriteLine ("Onspeechfinish utteranceid=" + Utteranceid);}
public void Onsynthesizestart (string Utteranceid) {Console.WriteLine ("Onsynthesizestart utteranceid=" + Utteranceid);} }
}

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.