欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

20.5 語(yǔ)音合成(百度2016年2月29日發(fā)布的tts引擎)

 更新時(shí)間:2016年03月17日 10:36:01   作者:rainmj  
編寫(xiě)手機(jī)App時(shí),有時(shí)需要使用文字轉(zhuǎn)語(yǔ)音(Text to Speech)的功能,比如開(kāi)車(chē)時(shí)閱讀收到的短信、導(dǎo)航語(yǔ)音提示、界面中比較重要的信息通過(guò)語(yǔ)音強(qiáng)調(diào)

一、簡(jiǎn)介

編寫(xiě)手機(jī)App時(shí),有時(shí)需要使用文字轉(zhuǎn)語(yǔ)音(Text to Speech)的功能,比如開(kāi)車(chē)時(shí)閱讀收到的短信、導(dǎo)航語(yǔ)音提示、界面中比較重要的信息通過(guò)語(yǔ)音強(qiáng)調(diào)、……等。

由于Android自帶的Pico TTS并不支持中文,所以要既能閱讀中文文本,還能閱讀英文文本,必須下載第三方提供的能說(shuō)中文的語(yǔ)音包。

二、申請(qǐng)百度tts授權(quán)

本節(jié)以百度2016年2月29日發(fā)布的“離在線融合語(yǔ)音合成SDK_Android 2.2.3版”為例說(shuō)明用C#實(shí)現(xiàn)語(yǔ)音合成的基本用法。之所以選擇百度語(yǔ)音合成來(lái)實(shí)現(xiàn),是因?yàn)閾?jù)百度官網(wǎng)聲明,該開(kāi)發(fā)包是“永久免費(fèi)”的。網(wǎng)址如下:

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

由于原來(lái)已經(jīng)申請(qǐng)過(guò)MyDemos的授權(quán),所以再繼續(xù)申請(qǐng)tts授權(quán)就比較簡(jiǎn)單了,申請(qǐng)和設(shè)置步驟如下。

1、申請(qǐng)授權(quán)

進(jìn)入 http://yuyin.baidu.com/tts/ 的首頁(yè):

單擊【立即使用】,進(jìn)入“開(kāi)通語(yǔ)音合成服務(wù)”的頁(yè)面:


在下拉框中選擇原來(lái)已經(jīng)申請(qǐng)的某個(gè)應(yīng)用,單擊【下一步】,然后按提示操作,開(kāi)通離線服務(wù)即可。

2、在BdMapV371BindingLib項(xiàng)目中轉(zhuǎn)換JAR文件

先通過(guò) http://yuyin.baidu.com/tts/ 首頁(yè)中的【相關(guān)下載】下載對(duì)應(yīng)的開(kāi)發(fā)包,然后再按下面的步驟操作。

1、將示例中的com.baidu.tts_2.2.3.20160229_359d952_release.jar、galaxy-v2.0.jar添加到Jars文件夾下,如下圖所示,然后將其【生成操作】屬性全部設(shè)置為“EmbeddedJar”。

2、在Metadata.xml文件中添加下面的語(yǔ)句:

<remove-node path="/api/package[@name='com.baidu.tts.aop']/interface[@name='IProxyFactory']/method[@name='createProxied' and count(parameter)=0]" /> 

3、重新生成項(xiàng)目,此時(shí)應(yīng)該無(wú)錯(cuò)誤。

經(jīng)過(guò)這3個(gè)步驟,就完成了tts的Jar包導(dǎo)入和轉(zhuǎn)換為.cs文件的過(guò)程。

3、在MyDemos項(xiàng)目中添加.so文件

將tts相關(guān)的4個(gè).so文件添加到MyDemos項(xiàng)目的x86文件夾下,如下圖所示,然后將其【生成操作】屬性全部設(shè)置為“AndroidNativeLibrary”。

4、將.dat文件添加到sd卡的BaiduTTS文件夾下

具體添加辦法見(jiàn)【常見(jiàn)問(wèn)題解答】,這里不再截圖。

也可以先將這些文件添加到Assets文件夾下,然后通過(guò)代碼將其復(fù)制到sd卡的文件夾下。為簡(jiǎn)化起見(jiàn),這里通過(guò)手工直接復(fù)制了。

OK,經(jīng)過(guò)上面這4步,以后就可以在MyDemos項(xiàng)目中的任何模塊中輕松利用百度tts實(shí)現(xiàn)語(yǔ)音閱讀的功能了

三、示例

1、運(yùn)行截圖

單擊【閱讀】,就會(huì)自動(dòng)用女音朗讀文本框中的內(nèi)容,單擊【批量閱讀】,就會(huì)依次朗讀隊(duì)列中添加的文字段(主要是為了演示閱讀各種不同的中英文短句)。

2、設(shè)計(jì)步驟

(1)添加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="閱讀"
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="暫停"
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="繼續(xù)"
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="停止"
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="批量閱讀"
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、添加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 = "【例20-5】百度tts基本用法")]
public class ch2005MainActivity : Activity, ISpeechSynthesizerListener
{
private EditText mInput;
private TextView mShowText;
private SpeechSynthesizer mSpeechSynthesizer;
/// <summary>
/// sd卡上保存百度tts文件的路徑
/// </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);
// 文本模型文件路徑 (離線引擎使用)
mSpeechSynthesizer.SetParam(SpeechSynthesizer.ParamTtsTextModelFile,
mSampleDirPath + "/" + TextModelName);
// 聲學(xué)模型文件路徑 (離線引擎使用)
mSpeechSynthesizer.SetParam(SpeechSynthesizer.ParamTtsSpeechModelFile,
mSampleDirPath + "/" + SpeechFemaleModelName);
// 請(qǐng)?zhí)鎿Q為語(yǔ)音開(kāi)發(fā)者平臺(tái)上注冊(cè)應(yīng)用得到的App ID (離線授權(quán))
//mSpeechSynthesizer.SetAppId("your_app_id");
mSpeechSynthesizer.SetAppId(ch.TtsAppID);
// 請(qǐng)?zhí)鎿Q為語(yǔ)音開(kāi)發(fā)者平臺(tái)注冊(cè)應(yīng)用得到的apikey和secretkey (在線授權(quán))
//this.mSpeechSynthesizer.SetApiKey("your_api_key", "your_secret_key");
this.mSpeechSynthesizer.SetApiKey(ch.TtsApiKey, ch.TtsSecretKey);
// 發(fā)音人(在線引擎),可用參數(shù)為0,1,2,3。。。(服務(wù)器端會(huì)動(dòng)態(tài)增加,各值含義參考文檔,以文檔說(shuō)明為準(zhǔn)。0--普通女聲,1--普通男聲,2--特別男聲,3--情感男聲。。。)
mSpeechSynthesizer.SetParam(SpeechSynthesizer.ParamSpeaker, "0");
// 設(shè)置Mix模式的合成策略
mSpeechSynthesizer.SetParam(SpeechSynthesizer.ParamMixMode, SpeechSynthesizer.MixModeDefault);
// 授權(quán)檢測(cè)接口(可以不使用,只是驗(yàn)證授權(quán)是否成功)
AuthInfo authInfo = this.mSpeechSynthesizer.Auth(TtsMode.Mix);
if (authInfo.IsSuccess)
{
Console.WriteLine("授權(quán)檢測(cè)--授權(quán)成功(auth success)。");
}
else
{
string errorMsg = authInfo.TtsError.DetailMessage;
Console.WriteLine("授權(quán)檢測(cè)--授權(quán)失敗(auth failed),errorMsg=" + errorMsg);
}
// 初始化tts
mSpeechSynthesizer.InitTts(TtsMode.Mix);
// 加載離線英文資源(提供離線英文合成功能)
int result = mSpeechSynthesizer.LoadEnglishModel(
mSampleDirPath +
"/" + EnglishTextModelName, mSampleDirPath +
"/" + EnglishSpeechFemaleModelName);
}
private void initialView()
{
mInput = FindViewById<EditText>(Resource.Id.input);
mInput.Text = "今天陽(yáng)光明媚,風(fēng)和日麗!";
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("出錯(cuò)了,錯(cuò)誤碼:{0},請(qǐng)檢查百度tts文檔中對(duì)應(yīng)錯(cuò)誤碼的含義。", 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/122 ");
}
};
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("你好", "1"));
bags.Add(GetSpeechSynthesizeBag("使用百度語(yǔ)音合成SDK", "2"));
bags.Add(GetSpeechSynthesizeBag("hello", "3"));
bags.Add(GetSpeechSynthesizeBag("這是一個(gè)demo工程", "4"));
int result = this.mSpeechSynthesizer.BatchSpeak(bags);
if (result < 0)
{
System.Diagnostics.Debug.WriteLine("error({0}),please look up error code in doc or URL:http://yuyin.baidu.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);
}
public void OnSpeechProgressChanged(string p0, int p1)
{
//Console.WriteLine("onSpeechProgressChanged");
}
public void OnSpeechStart(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);
}
}
}

相關(guān)文章

最新評(píng)論