Android TextToSpeech在服务中
我在Android TextToSpeech在服务中,android,service,text-to-speech,Android,Service,Text To Speech,我在服务中遇到了TTS问题。它好像想说话,但从来没有。观察LogCat,它打印“TTS接收:它应该说的文本”,我在它初始化时记录,这显示了成功。我试着为它创建一个线程,但没有用。 onutternancecomplete也不会触发。我甚至做过这样的while循环(只是为了测试): …而且它从不说话 我知道TTS设置正确,因为它在常规的活动中工作 这是我的密码 import java.util.HashMap; import java.util.Locale; import android.ap
服务中遇到了TTS
问题。它好像想说话,但从来没有。观察LogCat,它打印“TTS接收:它应该说的文本”,我在它初始化时记录,这显示了成功。我试着为它创建一个线程,但没有用。
onutternancecomplete
也不会触发。我甚至做过这样的while循环(只是为了测试):
…而且它从不说话
我知道TTS设置正确,因为它在常规的活动中工作
这是我的密码
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();
}
}
}
import java.util.HashMap;
导入java.util.Locale;
导入android.app.Service;
导入android.content.Intent;
导入android.media.AudioManager;
导入android.os.IBinder;
导入android.speech.tts.TextToSpeech;
导入android.speech.tts.TextToSpeech.OnInitListener;
导入android.speech.tts.TextToSpeech.onutternancecompletedlistener;
导入android.util.Log;
公共类TTSService扩展服务实现OnInitListener、OnPatternanceCompletedListener{
texttospeechmtts;
@凌驾
public void onCreate(){
Log.d(“,”TTSService Created!”);
mTTS=新的TextToSpeech(getApplicationContext(),this);
//我已经试过了。。。。
/*新线程(newrunnable()){
@凌驾
公开募捐{
HashMap myHashStream=新HashMap();
myHashStream.put(TextToSpeech.Engine.KEY_PARAM_STREAM,String.valueOf(AudioManager.STREAM_NOTIFICATION));
myHashStream.put(TextToSpeech.Engine.KEY_参数_话语_ID,“1”);
mTTS.setLanguage(Locale.US);
//mTTS.setOnPatternanceCompletedListener(此);
mTTS.speak(“我在跟你说一些东西!”,TextToSpeech.QUEUE\u FLUSH,myHashStream);
}
}).start()*/
//我一根线也没试过。。。
HashMap myHashStream=新HashMap();
myHashStream.put(TextToSpeech.Engine.KEY_PARAM_STREAM,String.valueOf(AudioManager.STREAM_NOTIFICATION));
myHashStream.put(TextToSpeech.Engine.KEY_参数_话语_ID,“1”);
mTTS.setLanguage(Locale.US);
mTTS.setOnPatternanceCompletedListener(此);
mTTS.speak(“我在跟你说一些东西!”,TextToSpeech.QUEUE\u FLUSH,myHashStream);
}
@凌驾
公共IBinder onBind(意向){
返回null;
}
@凌驾
公共无效onInit(int状态){
Log.d(“,”TTSService onInit:“+String.valueOf(status));
if(status==TextToSpeech.SUCCESS){
Log.d(“,“TTS成功”);
}
}
公共void onutternancecompleted(字符串uttId){
Log.d(“,”完成了);
如果(uttId==“1”){
mTTS.shutdown();
}
}
}
谢谢好的,我现在已经弄明白了!发生的事情是,它试图在初始化TTS
之前说话。所以在一个线程中,我等待ready to not==999。一旦是1或任何其他,我们将负责发言。把它放在一个while循环中可能不安全,但是。。。尽管如此,它仍在发挥作用
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;
int ready = 999;
@Override
public void onCreate() {
Log.d("", "TTSService Created!");
mTTS = new TextToSpeech(getApplicationContext(), this);
new Thread(new Runnable() {
@Override
public void run() {
while(ready == 999) {
//wait
}
if(ready==1){
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);
} else {
Log.d("", "not ready");
}
}
}).start();
stopSelf();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
mTTS.shutdown();
super.onDestroy();
}
@Override
public void onInit(int status) {
Log.d("", "TTSService onInit: " + String.valueOf(status));
if (status == TextToSpeech.SUCCESS)
{
ready = 1;
} else {
ready = 0;
Log.d("", "failed to initialize");
}
}
public void onUtteranceCompleted(String uttId) {
Log.d("", "done uttering");
if(uttId == "1") {
mTTS.shutdown();
}
}
}
import java.util.HashMap;
导入java.util.Locale;
导入android.app.Service;
导入android.content.Intent;
导入android.media.AudioManager;
导入android.os.IBinder;
导入android.speech.tts.TextToSpeech;
导入android.speech.tts.TextToSpeech.OnInitListener;
导入android.speech.tts.TextToSpeech.onutternancecompletedlistener;
导入android.util.Log;
公共类TTSService扩展服务实现OnInitListener、OnPatternanceCompletedListener{
texttospeechmtts;
int ready=999;
@凌驾
public void onCreate(){
Log.d(“,”TTSService Created!”);
mTTS=新的TextToSpeech(getApplicationContext(),this);
新线程(newrunnable()){
@凌驾
公开募捐{
while(就绪==999){
//等等
}
如果(就绪==1){
HashMap myHashStream=新HashMap();
myHashStream.put(TextToSpeech.Engine.KEY_PARAM_STREAM,String.valueOf(AudioManager.STREAM_NOTIFICATION));
myHashStream.put(TextToSpeech.Engine.KEY_参数_话语_ID,“1”);
mTTS.setLanguage(Locale.US);
//mTTS.setOnPatternanceCompletedListener(此);
mTTS.speak(“我在跟你说一些东西!”,TextToSpeech.QUEUE\u FLUSH,myHashStream);
}否则{
Log.d(“,”未准备就绪“);
}
}
}).start();
stopSelf();
}
@凌驾
公共IBinder onBind(意向){
返回null;
}
@凌驾
公共空间{
mTTS.shutdown();
super.ondestory();
}
@凌驾
公共无效onInit(int状态){
Log.d(“,”TTSService onInit:“+String.valueOf(status));
if(status==TextToSpeech.SUCCESS)
{
就绪=1;
}否则{
就绪=0;
Log.d(“,”初始化失败”);
}
}
公共void onutternancecompleted(字符串uttId){
Log.d(“,”完成了);
如果(uttId==“1”){
mTTS.shutdown();
}
}
}
好的,我现在已经弄明白了!发生的事情是,它试图在初始化TTS
之前说话。所以在一个线程中,我等待ready to not==999。一旦是1或任何其他,我们将负责发言。把它放在一个while循环中可能不安全,但是。。。尽管如此,它仍在发挥作用
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;
int ready = 999;
@Override
public void onCreate() {
Log.d("", "TTSService Created!");
mTTS = new TextToSpeech(getApplicationContext(), this);
new Thread(new Runnable() {
@Override
public void run() {
while(ready == 999) {
//wait
}
if(ready==1){
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);
} else {
Log.d("", "not ready");
}
}
}).start();
stopSelf();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
mTTS.shutdown();
super.onDestroy();
}
@Override
public void onInit(int status) {
Log.d("", "TTSService onInit: " + String.valueOf(status));
if (status == TextToSpeech.SUCCESS)
{
ready = 1;
} else {
ready = 0;
Log.d("", "failed to initialize");
}
}
public void onUtteranceCompleted(String uttId) {
Log.d("", "done uttering");
if(uttId == "1") {
mTTS.shutdown();
}
}
}
import java.util.HashMap;
导入java.util.Locale;
导入android.app.Service;
导入android.content.Intent;
导入android.media.AudioManager;
导入android.os.IBinder;
导入android.speech.tts.TextToSpeech;
导入android.speech.tts.TextToSpeech.OnInitListener;
导入android.speech.tts.TextToSpeech.onutternancecompletedlistener;
导入android.util.Log;
公共类TTS服务扩展服务