服务中的文本转语音
我在 Service
中遇到 TTS
问题。它表现得好像想要说话,但它从来没有说话。观察 LogCat,它会打印“TTS 已收到:它应该说出的文本”,并且我会在初始化时进行记录,这表明成功。我尝试为其创建一个线程,但这没有帮助。 onUtteranceComplete
也永远不会触发。我什至做了一个像这样的 while 循环(仅用于测试):
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();
}
}
}
谢谢
I've got a problem with TTS
in a Service
. It acts like it wants to talk but it never does. Watching the LogCat it prints "TTS received: the text it should speak" and I Log when it init's and that's showing success. I've tried creating a thread for it, that didnt help.onUtteranceComplete
never triggers either. I've even done a while loop like this (just for testing):
while(mTTS.isSpeaking()) {
Log.d("", "speaking");
}
...and it's never speaking
I know TTS is setup correctly because it works in a regular Activity
Here's my code.
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();
}
}
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,我现在已经弄清楚了!发生的情况是它在初始化 TTS 之前尝试说话。因此,在一个线程中,我等待准备好不 == 999。一旦它为 1 或其他任何值,我们将负责说话。将其放入 while 循环中可能不安全,但是......它仍然有效。
Ok, I've got it figured out now! What was happening is it was trying to speak before
TTS
was initialized. So in a thread I wait for ready to not == 999. Once its either 1 or anything else we'll then take care of speaking. This might not be safe putting it in a while loop but... It's working nonetheless.我刚刚遇到了类似的问题 - 这是由于在
onInit()
启动 TTS 之前调用了mTTS.speak
。我的处理方式略有不同,将mTTS.speak()
放在一个单独的函数中,然后从onInit if (TextToSpeech.Successful)
中调用该函数,因此播放语音 <启动之后并且启动之前不(来自onStart()
等)。I've just had a similar issue - It was due to
mTTS.speak
being called before TTS was initiated byonInit()
. I tackled it slightly differently by placingmTTS.speak()
in a separate function and then calling that function from withinonInit if (TextToSpeech.Successful)
, therefore playing the speech after it is initiated and not before (fromonStart()
ect).