20.5 語音合成(百度2016年2月29日發(fā)布的tts引擎)
一、簡介
編寫手機App時,有時需要使用文字轉語音(Text to Speech)的功能,比如開車時閱讀收到的短信、導航語音提示、界面中比較重要的信息通過語音強調、……等。
由于Android自帶的Pico TTS并不支持中文,所以要既能閱讀中文文本,還能閱讀英文文本,必須下載第三方提供的能說中文的語音包。
二、申請百度tts授權
本節(jié)以百度2016年2月29日發(fā)布的“離在線融合語音合成SDK_Android 2.2.3版”為例說明用C#實現(xiàn)語音合成的基本用法。之所以選擇百度語音合成來實現(xiàn),是因為據百度官網聲明,該開發(fā)包是“永久免費”的。網址如下:
http://yuyin.baidu.com/tts/
由于原來已經申請過MyDemos的授權,所以再繼續(xù)申請tts授權就比較簡單了,申請和設置步驟如下。
1、申請授權
進入 http://yuyin.baidu.com/tts/ 的首頁:
單擊【立即使用】,進入“開通語音合成服務”的頁面:
在下拉框中選擇原來已經申請的某個應用,單擊【下一步】,然后按提示操作,開通離線服務即可。
2、在BdMapV371BindingLib項目中轉換JAR文件
先通過 http://yuyin.baidu.com/tts/ 首頁中的【相關下載】下載對應的開發(fā)包,然后再按下面的步驟操作。
1、將示例中的com.baidu.tts_2.2.3.20160229_359d952_release.jar、galaxy-v2.0.jar添加到Jars文件夾下,如下圖所示,然后將其【生成操作】屬性全部設置為“EmbeddedJar”。
2、在Metadata.xml文件中添加下面的語句:
<remove-node path="/api/package[@name='com.baidu.tts.aop']/interface[@name='IProxyFactory']/method[@name='createProxied' and count(parameter)=0]" />
3、重新生成項目,此時應該無錯誤。
經過這3個步驟,就完成了tts的Jar包導入和轉換為.cs文件的過程。
3、在MyDemos項目中添加.so文件
將tts相關的4個.so文件添加到MyDemos項目的x86文件夾下,如下圖所示,然后將其【生成操作】屬性全部設置為“AndroidNativeLibrary”。
4、將.dat文件添加到sd卡的BaiduTTS文件夾下
具體添加辦法見【常見問題解答】,這里不再截圖。
也可以先將這些文件添加到Assets文件夾下,然后通過代碼將其復制到sd卡的文件夾下。為簡化起見,這里通過手工直接復制了。
OK,經過上面這4步,以后就可以在MyDemos項目中的任何模塊中輕松利用百度tts實現(xiàn)語音閱讀的功能了
三、示例
1、運行截圖
單擊【閱讀】,就會自動用女音朗讀文本框中的內容,單擊【批量閱讀】,就會依次朗讀隊列中添加的文字段(主要是為了演示閱讀各種不同的中英文短句)。
2、設計步驟
(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);
// 聲學模型文件路徑 (離線引擎使用)
mSpeechSynthesizer.SetParam(SpeechSynthesizer.ParamTtsSpeechModelFile,
mSampleDirPath + "/" + SpeechFemaleModelName);
// 請?zhí)鎿Q為語音開發(fā)者平臺上注冊應用得到的App ID (離線授權)
//mSpeechSynthesizer.SetAppId("your_app_id");
mSpeechSynthesizer.SetAppId(ch.TtsAppID);
// 請?zhí)鎿Q為語音開發(fā)者平臺注冊應用得到的apikey和secretkey (在線授權)
//this.mSpeechSynthesizer.SetApiKey("your_api_key", "your_secret_key");
this.mSpeechSynthesizer.SetApiKey(ch.TtsApiKey, ch.TtsSecretKey);
// 發(fā)音人(在線引擎),可用參數(shù)為0,1,2,3。。。(服務器端會動態(tài)增加,各值含義參考文檔,以文檔說明為準。0--普通女聲,1--普通男聲,2--特別男聲,3--情感男聲。。。)
mSpeechSynthesizer.SetParam(SpeechSynthesizer.ParamSpeaker, "0");
// 設置Mix模式的合成策略
mSpeechSynthesizer.SetParam(SpeechSynthesizer.ParamMixMode, SpeechSynthesizer.MixModeDefault);
// 授權檢測接口(可以不使用,只是驗證授權是否成功)
AuthInfo authInfo = this.mSpeechSynthesizer.Auth(TtsMode.Mix);
if (authInfo.IsSuccess)
{
Console.WriteLine("授權檢測--授權成功(auth success)。");
}
else
{
string errorMsg = authInfo.TtsError.DetailMessage;
Console.WriteLine("授權檢測--授權失?。╝uth 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 = "今天陽光明媚,風和日麗!";
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("出錯了,錯誤碼:{0},請檢查百度tts文檔中對應錯誤碼的含義。", 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("使用百度語音合成SDK", "2"));
bags.Add(GetSpeechSynthesizeBag("hello", "3"));
bags.Add(GetSpeechSynthesizeBag("這是一個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);
}
}
}
相關文章
Android圖片異步加載框架Android-Universal-Image-Loader
這篇文章主要介紹了Android圖片異步加載框架Android-Universal-Image-Loader,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
Android發(fā)送GET與POST請求的DEMO詳解
本篇文章是對Android發(fā)送GET與POST請求的DEMO進行了詳細的分析介紹,需要的朋友參考下2013-06-06
Android編程監(jiān)聽APK安裝與刪除等過程的方法
這篇文章主要介紹了Android編程監(jiān)聽APK安裝與刪除等過程的方法,涉及Android事件監(jiān)聽、權限控制、廣播操作等相關實現(xiàn)技巧,需要的朋友可以參考下2017-10-10
Android?APN數(shù)據庫查詢對比分析(APN案例)
文章詳細介紹了Android中APN數(shù)據查詢的實現(xiàn)方式,文章說明了如何避免在主線程進行IO操作,從而提高應用的響應性和用戶體驗,感興趣的朋友一起看看吧2025-03-03






