Monday, January 21, 2013

Android sample source–TTS 科大讯飞

科大讯飞在语音新闻王的集成源码如下:

package pk17s.cn;

import org.apache.cordova.CordovaWebView;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.webkit.WebView;

import com.iflytek.speech.SpeechError;
import com.iflytek.speech.SynthesizerPlayer;
import com.iflytek.speech.SynthesizerPlayerListener;

public class KdxfTTS implements SynthesizerPlayerListener {
private SynthesizerPlayer mSynthesizerPlayer;
CallbackContext callbackContext;
private CordovaPlugin plugin;

public KdxfTTS(Context context,CordovaPlugin _plugin){
plugin = _plugin;
mSynthesizerPlayer = SynthesizerPlayer.createSynthesizerPlayer(
context, "appid=4dabf812");
}

public Boolean speakLanauageText(String locale,String role,String text,CallbackContext _callbackContext){

callbackContext = _callbackContext;

mSynthesizerPlayer.setVoiceName(role);

mSynthesizerPlayer.playText(text, null,this);

return true;
}

public Boolean speakLanauageTextToFile(String locale,String role,String text,CallbackContext _callbackContext){

callbackContext = _callbackContext;

mSynthesizerPlayer.setVoiceName(role);

mSynthesizerPlayer.playText(text, null,this);

return true;
}

public void setMute(Boolean muted){

}

public void setVolume(Long volume){

}

public void getLanauageList(JSONArray lanauageList){
try{
JSONArray lanauages = new JSONArray();
JSONObject lanauage = new JSONObject();

lanauage.put("id", "xiaoyan");lanauage.put("name", "讯飞-晓燕");lanauages.put(lanauage);lanauage = new JSONObject();
lanauage.put("id", "xiaoyu");lanauage.put("name", "讯飞-晓宇");lanauages.put(lanauage);lanauage = new JSONObject();
lanauage.put("id", "vixy");lanauage.put("name", "讯飞-小研");lanauages.put(lanauage);lanauage = new JSONObject();
lanauage.put("id", "vixq");lanauage.put("name", "讯飞-小琪");lanauages.put(lanauage);lanauage = new JSONObject();
lanauage.put("id", "vixf");lanauage.put("name", "讯飞-小峰");lanauages.put(lanauage);lanauage = new JSONObject();
lanauage.put("id", "vixm");lanauage.put("name", "讯飞-小梅");lanauages.put(lanauage);lanauage = new JSONObject();
lanauage.put("id", "vixl");lanauage.put("name", "讯飞-小莉");lanauages.put(lanauage);lanauage = new JSONObject();
lanauage.put("id", "vixr");lanauage.put("name", "讯飞-小蓉");lanauages.put(lanauage);lanauage = new JSONObject();
lanauage.put("id", "vixyun");lanauage.put("name", "讯飞-小芸");lanauages.put(lanauage);lanauage = new JSONObject();
lanauage.put("id", "vixk");lanauage.put("name", "讯飞-小坤");lanauages.put(lanauage);lanauage = new JSONObject();
lanauage.put("id", "vixqa");lanauage.put("name", "讯飞-小强 ");lanauages.put(lanauage);lanauage = new JSONObject();
lanauage.put("id", "vixying");lanauage.put("name", "讯飞-小莹");lanauages.put(lanauage);lanauage = new JSONObject();
lanauage.put("id", "vixx");lanauage.put("name", "讯飞-小新");lanauages.put(lanauage);lanauage = new JSONObject();
lanauage.put("id", "vinn");lanauage.put("name", "讯飞-楠楠");lanauages.put(lanauage);lanauage = new JSONObject();
lanauage.put("id", "vils");lanauage.put("name", "讯飞-老孙");lanauages.put(lanauage);

JSONObject locale = new JSONObject();
locale.put("locale","zh_CN");
locale.put("tts",lanauages);
locale.put("downloadCapable",false);
lanauageList.put(locale);

locale = new JSONObject();
locale.put("locale","zh_TW");
locale.put("tts",lanauages);
locale.put("downloadCapable",false);
lanauageList.put(locale);

locale = new JSONObject();
locale.put("locale","zh");
locale.put("tts",lanauages);
locale.put("downloadCapable",false);
lanauageList.put(locale);

locale = new JSONObject();
locale.put("locale","en");
locale.put("tts",lanauages);
locale.put("downloadCapable",false);
lanauageList.put(locale);
}catch(Exception e){

}
}

public Boolean Stop(){
if (null != mSynthesizerPlayer) {
mSynthesizerPlayer.cancel();
}
return true;
}

protected void onStop() {
if (null != mSynthesizerPlayer) {
mSynthesizerPlayer.cancel();
}
}

public void onBufferPercent(int arg0, int arg1, int arg2) {
// TODO Auto-generated method stub

}

public void onEnd(SpeechError arg0) {
// TODO Auto-generated method stub
if(arg0 == null){
if(callbackContext!=null){
PluginResult result = new PluginResult(PluginResult.Status.OK);
result.setKeepCallback(false);
callbackContext.sendPluginResult(result);
callbackContext = null;
}
}
}

public void onPlayBegin() {
// TODO Auto-generated method stub

}

public void onPlayPaused() {
// TODO Auto-generated method stub

}

public void onPlayPercent(int arg0, int arg1, int arg2) {
// TODO Auto-generated method stub

}

public void onPlayResumed() {
// TODO Auto-generated method stub

}
}

No comments: