
科?訊飛離線語?合成簡單實例
如何使?科?訊飛開放平臺的語?合成呢?
1.?先要注冊開發者賬戶,創建我的新應?,創建完后就有?個專屬APPID(?常重要),添加我的應?,下載你所需要的sdk,這?我們下
載離線語?合成。注意:下載的sdk?件,都是??創建的應??成的,申請appid時就與??的應?綁定了的。所以?定要使???下載的
?件,appid的值也要使???的。
2.下載后將sdk解壓,將libs下的兩個jar包添加到libs?錄下(jar包還要右鍵addtoLibraries),同路徑下的其它.so?件(與c進?交互)復
制到main路徑下新建的jniLibs(L要?寫)?錄下,將asrt?錄拷貝到main?錄下,將res?錄下的tts?件復制到asrt?錄下。并在你的
繼承了Application的類中添加下圖的??代碼進?配置(appid的值寫??注冊的)。這些?件的安放位置在我的項?中體現如下:
3.在MainActivity同?錄下創建類KqwSpeechSynthesizer,并創建類SoundTipUtil,再通過下圖?框圈出的代碼,即可實現在activity內
進?語?合成的使?了。不妨試試!
KqwSpeechSynthesizer類的代碼如下:
t;
;
;
;
ode;
stener;
Constant;
Error;
Synthesizer;
sizerListener;
ceUtil;
/**
*語?合成器
*
*@authorkongqw
*/
publicclassKqwSpeechSynthesizer{
//Log標簽
privatestaticfinalStringTAG="KqwSpeechSynthesizer";
privateContextmContext;
//語?合成對象
privateSpeechSynthesizermTts;
publicKqwSpeechSynthesizer(Contextcontext){
mContext=context;
//初始化合成對象
mTts=Synthesizer(context,newInitListener(){
@Override
publicvoidonInit(intcode){
Log.d(TAG,"InitListenerinit()code="+code);
if(code!=S){
xt(mContext,"初始化失敗,錯誤碼:"+code,_SHORT).show();
}
}
});
}
/**
*開始語?合成
*
*@paramtext
*/
publicvoidstart(Stringtext){
//設置參數
tParam();
intcode=peaking(text,mTtsListener);
if(code!=S){
xt(mContext,"語?合成失敗,錯誤碼:"+code,_SHORT).show();
}
}
/**
*合成回調監聽。
*/
privateSynthesizerListenermTtsListener=newSynthesizerListener(){
@Override
publicvoidonSpeakBegin(){
Log.i(TAG,"開始合成");
}
@Override
publicvoidonSpeakPaud(){
Log.i(TAG,"暫停合成");
}
@Override
publicvoidonSpeakResumed(){
Log.i(TAG,"繼續合成");
}
@Override
publicvoidonBufferProgress(intpercent,intbeginPos,intendPos,Stringinfo){
Log.i(TAG,"傳沖進度:"+percent);
}
@Override
publicvoidonSpeakProgress(intpercent,intbeginPos,intendPos){
Log.i(TAG,"合成進度:"+percent);
}
@Override
publicvoidonCompleted(SpeechErrorerror){
if(error==null){
Log.i(TAG,"合成完成");
}elif(error!=null){
Log.i(TAG,"error:"+ng());
}
}
@Override
publicvoidonEvent(intarg0,intarg1,intarg2,Bundlearg3){
//TODOAuto-generatedmethodstub
}
};
/**
*參數設置
*
*@return
*@return
*/
privatevoidtParam(){
//清空參數
ameter(,null);
//設置使?本地引擎
ameter(_TYPE,_LOCAL);
//設置發??資源路徑
ameter(_RES_PATH,getResourcePath());
//設置發??
ameter(_NAME,"xiaoyan");
//設置語速
ameter(,"50");
//設置?調
ameter(,"50");
//設置?量
ameter(,"50");
//設置播放器?頻流類型
ameter(_TYPE,"3");
}
//獲取發??資源路徑
privateStringgetResourcePath(){
StringBuffertempBuffer=newStringBuffer();
//合成通?資源
(teResourcePath(mContext,CE_,"tts/"));
(";");
//發??資源
(teResourcePath(mContext,CE_,"tts/"));
ng();
}
}
SoundTipUtill類代碼如下:
t;
publicclassSoundTipUtil{
privatestaticKqwSpeechSynthesizerkqwSpeechSynthesizer;
publicstaticvoidsoundTip(Contextcontext,Stringtext){
kqwSpeechSynthesizer=newKqwSpeechSynthesizer(context);
(text);
}
}
如果不是在activity內進?語?播報,那該咋辦?
按以下步驟即可實現:1.在繼承了Application的類中添加如下??代碼(圖1);
2.在?Activity類中調??法即可(如圖2)
本文發布于:2023-03-10 16:19:00,感謝您對本站的認可!
本文鏈接:http://www.newhan.cn/zhishi/a/1678436341135186.html
版權聲明:本站內容均來自互聯網,僅供演示用,請勿用于商業和其他非法用途。如果侵犯了您的權益請與我們聯系,我們將在24小時內刪除。
本文word下載地址:訊飛語音.doc
本文 PDF 下載地址:訊飛語音.pdf
| 留言與評論(共有 0 條評論) |