20.5 语音合成(百度2016年2月29日发布的tts引擎)

20.5 语音合成(百度2016年2月29日发布的tts引擎),第1张

概述一、简介编写手机App时,有时需要使用文字转语音(TexttoSpeech)的功能,比如开车时阅读收到的短信、导航语音提示、界面中比较重要的信息通过语音强调、……等。

一、简介

编写手机App时,有时需要使用文字转语音(Text to Speech)的功能,比如开车时阅读收到的短信、导航语音提示、界面中比较重要的信息通过语音强调、……等。

由于AndroID自带的Pico TTS并不支持中文,所以要既能阅读中文文本,还能阅读英文文本,必须下载第三方提供的能说中文的语音包。

二、申请百度tts授权

本节以百度2016年2月29日发布的“离在线融合语音合成SDK_AndroID 2.2.3版”为例说明用C#实现语音合成的基本用法。之所以选择百度语音合成来实现,是因为据百度官网声明,该开发包是“永久免费”的。网址如下:

http://yuyin.baIDu.com/tts/

由于原来已经申请过MyDemos的授权,所以再继续申请tts授权就比较简单了,申请和设置步骤如下。

1、申请授权

进入 http://yuyin.baIDu.com/tts/ 的首页:

单击【立即使用】,进入“开通语音合成服务”的页面:


在下拉框中选择原来已经申请的某个应用,单击【下一步】,然后按提示 *** 作,开通离线服务即可。

2、在BdMapV371Bindinglib项目中转换JAR文件

先通过 http://yuyin.baIDu.com/tts/ 首页中的【相关下载】下载对应的开发包,然后再按下面的步骤 *** 作。

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实现语音阅读的功能了

三、示例

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"><linearLayoutandroID:layout_wIDth="fill_parent"androID:layout_height="50dp"androID:orIEntation="horizontal"androID:weightSum="4"><buttonandroID: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" /><buttonandroID: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" /><buttonandroID:ID="@+ID/resume"androID:layout_wIDth="fill_parent"androID:layout_height="fill_parent"androID:layout_weight="1"androID:lines="2"androID:text="继续"androID:textSize="12dp" /><buttonandroID: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><linearLayoutandroID:layout_wIDth="fill_parent"androID:layout_height="50dp"androID:orIEntation="horizontal"androID:weightSum="4"><buttonandroID: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" /><buttonandroID: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" /><buttonandroID: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" /><buttonandroID: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><EditTextandroID:ID="@+ID/input"androID:layout_wIDth="fill_parent"androID:layout_height="wrap_content"androID:hint="input" /><TextVIEwandroID: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);// 请替换为语音开发者平台上注册应用得到的App ID (离线授权)//mSpeechSynthesizer.SetAppID("your_app_ID");mSpeechSynthesizer.SetAppID(ch.TtsAppID);// 请替换为语音开发者平台注册应用得到的APIkey和secretkey (在线授权)//this.mSpeechSynthesizer.SetAPIKey("your_API_key","your_secret_key");this.mSpeechSynthesizer.SetAPIKey(ch.TtsAPIKey,ch.TtsSecretKey);// 发音人(在线引擎),可用参数为0,1,2,3。。。(服务器端会动态增加,各值含义参考文档,以文档说明为准。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("授权检测--授权失败(auth Failed),errorMsg=" + errorMsg);}// 初始化ttsmSpeechSynthesizer.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);}}}

总结

以上是内存溢出为你收集整理的20.5 语音合成(百度2016年2月29日发布的tts引擎)全部内容,希望文章能够帮你解决20.5 语音合成(百度2016年2月29日发布的tts引擎)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1149570.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存