Android 从服务启动文本到语音引擎?
我有一个服务,我试图从那里启动TextToSpeech引擎,但它似乎不工作,所以有可能从服务启动tts吗 以下是我尝试过的:Android 从服务启动文本到语音引擎?,android,Android,我有一个服务,我试图从那里启动TextToSpeech引擎,但它似乎不工作,所以有可能从服务启动tts吗 以下是我尝试过的: package com.example.TextSpeaker; import java.util.Locale; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.speech.tts.TextToSpeech; i
package com.example.TextSpeaker;
import java.util.Locale;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.widget.Toast;
public class SpeakerService extends Service implements OnInitListener{
public static TextToSpeech mtts;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate(){
Log.d("SpeakerService","Service created successfully!");
mtts = new TextToSpeech(this,this);
mtts.setLanguage(Locale.ENGLISH);
}
@Override
public void onStart(Intent intent,int startid)
{
Log.d("SpeakerService","Service started successfully!");
Log.d("SpeakerService","Service started successfully!");
Log.d("SpeakerService","tspker.mtts = " + TextSpeaker.mtts.toString());
mtts = new TextToSpeech(this,this);
mtts.setLanguage(Locale.ENGLISH);
mtts.speak(Receiver.str, TextToSpeech.QUEUE_FLUSH,null);
}
@Override
public void onDestroy(){
if(mtts!=null)
{
mtts.stop();
Toast.makeText(getApplicationContext(),"The service has been destroyed!", T oast.LENGTH_SHORT).show();
}
}
@Override
public void onInit(int arg0) {
// TODO Auto-generated method stub
}
}
新建TextToSpeech(getApplicationContext(),this)
和我一起工作。。。
但一定要让服务运行,直到语音结束。“但一定要让服务运行,直到语音结束。.”您的意思是,如果我不通过设置手动终止服务,它将运行吗?发件人:不使用时。。。文本到语音功能依赖于在使用该功能的所有应用程序中共享的专用服务。当您使用完TTS后,做一个好公民,并通过调用mTts.shutdown()告诉它“您将不再需要它的服务”,例如在Activity onDestroy()方法中。