我正在制作一个应用程序,它使用tts来合成wav文件,每次一个广播接收器被触发。我正在使用AndroidStudio (最新的),并使用API 19和最小15。
我有带BroadcastListener的服务。每次执行BroadcastListener onReceive方法时,我都会使用
tts.synthesizeToFile(playing, null, storagePath + "/" + "tst.wav");
该文件已成功创建,但从未调用UtteranceProgressListener的UtteranceProgressListener()方法。
public void onCreate() {
super.onCreate();
//Get TTS capabilities
//TODO: Use TextToSpeech.Engine.ACTION_CHECK_TTS_DATA to check if tts is available
tts = new TextToSpeech(PresenterService.this,
new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
//If the TTS engine was started successfully
if (status != TextToSpeech.ERROR) {
tts.setLanguage(Locale.US);
tts.setPitch(PRESENTER_PITCH);
tts.setSpeechRate(PRESENTER_RATE);
}
}
});
tts.setOnUtteranceProgressListener(new TtsUtteranceListener());
IntentFilter filter = new IntentFilter();
filter.addAction("xxxxxxxxxxxxxxxxxx");
mReceiver = new TrackChangedReceiver(tts);
registerReceiver(mReceiver, filter);
}TtsUtteranceListener类:
public class TtsUtteranceListener extends UtteranceProgressListener {
@Override
public void onDone(String utteranceId) {
Log.d("TtsUtteranceListener", "utterance Done: " + utteranceId);
}
@Override
public void onStart(String utteranceId) {
Log.d("TtsUtteranceListener", "utterance Start: " + utteranceId);
}
@Override
public void onError(String utteranceId) {
Log.d("TtsUtteranceListener", "utterance Error: " + utteranceId);
}
}侦听器上的方法:
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action != null) {
if (action.equalsIgnoreCase("xxxxxxxxxxxxxxxxx")) {
String playing = "test string";
tts.synthesizeToFile(playing, null, storagePath + "/" + "tst.wav");
}
}
}我使用的是AndroidStudio,在调试模式下,我可以看到tts对象的mUtteranceProgressListener有一些对象引用:
com.example.android.ttstest.TtsUtteranceListener@41eaf8d8但是听众的方法从来没有被调用过。Log.d()调用永远无法工作,并且那里的任何断点都不会被触发。
在调用时,我还尝试将UtteranceProgressListener声明为匿名类。
tts.setOnUtteranceProgressListener(new UtteranceProgressListener(){...});但同样的事..。
有人能指出我做错了什么吗?
发布于 2014-02-18 22:01:45
好吧,我解决了。问题是我没有通过KEY_PARAM_UTTERANCE_ID考试。
if (action.equalsIgnoreCase("xxxxxxxxxxxxxxxxx")) {
HashMap<String, String> hashTts = new HashMap<String, String>();
hashTts.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "id");
String playing = "test string";
tts.synthesizeToFile(playing, hashTts, storagePath + "/" + "tst.wav");
}我将参数参数作为null传递:
tts.synthesizeToFile(playing, null, storagePath + "/" + "tst.wav");发布于 2017-09-18 20:11:36
我有一个有线的解决方案来检测tts是否已经发言完毕。虽然它不是优化的,但工作很好。
步骤1:在代码中创建这个类。(代码相同,而不是单独的文件)
class Waiter extends AsyncTask<Void,Void,Void>{
@Override
protected Void doInBackground(Void... voids) {
while (tts.isSpeaking()){
try{Thread.sleep(1000);}catch (Exception e){}
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
//TTS has finished speaking. WRITE YOUR CODE HERE
}
}步骤2:调用tts.speak(.)时调用它
tts.speak(...);
new Waiter().execute();https://stackoverflow.com/questions/21840688
复制相似问题