我在Service
中遇到了TTS
的问题。它的行为就像它想说话但它从来没有。看着它打印的“收到的TTS:它应该说的文本”LogCat,当它初始化时就会记录并显示成功。我试图为它创建一个线程,但没有帮助。 onUtteranceComplete
从来没有触发过。我甚至做过这样的(只是用于测试)while循环:TextToSpeech in a服务
while(mTTS.isSpeaking()) {
Log.d("", "speaking");
}
...它从来没有说
我知道TTS设置正确,因为它的工作原理在常规Activity
这是我的代码。
import java.util.HashMap;
import java.util.Locale;
import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.os.IBinder;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener;
import android.util.Log;
public class TTSService extends Service implements OnInitListener, OnUtteranceCompletedListener {
TextToSpeech mTTS;
@Override
public void onCreate() {
Log.d("", "TTSService Created!");
mTTS = new TextToSpeech(getApplicationContext(), this);
//I've tried it in a thread....
/*new Thread(new Runnable() {
@Override
public void run() {
HashMap<String, String> myHashStream = new HashMap<String, String>();
myHashStream.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_NOTIFICATION));
myHashStream.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "1");
mTTS.setLanguage(Locale.US);
//mTTS.setOnUtteranceCompletedListener(this);
mTTS.speak("I'm saying some stuff to you!", TextToSpeech.QUEUE_FLUSH, myHashStream);
}
}).start();*/
//I've tried it not in a thread...
HashMap<String, String> myHashStream = new HashMap<String, String>();
myHashStream.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_NOTIFICATION));
myHashStream.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "1");
mTTS.setLanguage(Locale.US);
mTTS.setOnUtteranceCompletedListener(this);
mTTS.speak("I'm saying some stuff to you!", TextToSpeech.QUEUE_FLUSH, myHashStream);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onInit(int status) {
Log.d("", "TTSService onInit: " + String.valueOf(status));
if(status == TextToSpeech.SUCCESS){
Log.d("", "TTS Success");
}
}
public void onUtteranceCompleted(String uttId) {
Log.d("", "done uttering");
if(uttId == "1") {
mTTS.shutdown();
}
}
}
感谢
我没有得到这个工作。我现在将服务中的信息传递给我的活动,如果它处于活动状态。 – Ifor
...是的。我没有可以传递给它的活动。 – bwoogie