Running Android TTS in a Service android tts 中文
up vote0down votefavorite
I'm trying to get Android's TTS to run inside a service, but I have no idea why it isn't working, it compiles, doesn't crash, but it just doesn't work.
The Toast notification do work though.
package alarm.test;
import android.app.Service;
import com.google.tts.TextToSpeechBeta;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class MyAlarmService extends Service {
private TextToSpeechBeta myTts;
private TextToSpeechBeta.OnInitListener ttsInitListener = new TextToSpeechBeta.OnInitListener() {
public void onInit( int arg0, int arg1 ) {
myTts.speak("", 0, null);
}
};
@Override
public void onCreate() {
// TODO Auto-generated method stub
myTts = new TextToSpeechBeta( this,
ttsInitListener );
Toast.makeText(this, "MyAlarmService.onCreate()", Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
myTts.speak("something is working", TextToSpeechBeta.QUEUE_FLUSH, null);
Toast.makeText(this, "MyAlarmService.onBind()", Toast.LENGTH_LONG).show();
return null;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(this, "MyAlarmService.onDestroy()", Toast.LENGTH_LONG).show();
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Toast.makeText(this, "MyAlarmService.onStart()", Toast.LENGTH_LONG).show();
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(this, "MyAlarmService.onUnbind()", Toast.LENGTH_LONG).show();
return super.onUnbind(intent);
}
}
android service tts
link|improve this question
asked Mar 7 at 12:12
Randy Davis
1
Please describe in more detail what you mean by "don't work". What do you see on LogCat? – Android Eve Mar 7 at 12:16
DEBUG/TextToSpeechBeta(512): TextToSpeechBeta not instled - dfauing to basic platform TextToSpeech for speak VERBOSE/TtsSice(418): TTS service received INFO/TtsService(418): Stoping INFO/TtsService(418): Stopped VERBOSE/TtsService(418): TTS procesing: VERBOSE/TtsService(418): TtsService.setLanguage(eng, USA, ) INFO/SVOX Pico Engine(418): Language already loaded (en-US == en-US) DEBUG/dalvikvm(68): GC_CONCURRENT freed 719K, 48% free 4093K/7751K, external 2041K/2378K, paused 21ms+27ms DEBUG/SntpClient(68): request time failed: java.net.SocketException: Address family not supported by protocol – Randy Davis Mar 7 at 19:35
feedback
2 Answers
activeoldestvotes
up vote0down vote
Well, making this question gave me a better answer than I could find with google.
Go to the answer to this question, and change it to fit your application.
TTS doesn't speak from a service whereas it does it from an activity in android
link|improve this answer
answered Mar 8 at 4:58
Randy Davis
1
feedback
up vote0down vote
Hi man you can do like this:it's work for me. you must create a a activity to start this service ,like this :this.startService(intent)
public class TTSService extends Service implements TextToSpeech.OnInitListener{
private String str;
private TextToSpeech mTts;
private static final String TAG="TTSService";
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
mTts = new TextToSpeech(this,
this // OnInitListener
);
mTts.setSpeechRate(0.5f);
Log.v(TAG, "oncreate_service");
str ="turn left please ";
super.onCreate();
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
if (mTts != null) {
mTts.stop();
mTts.shutdown();
}
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
sayHello(str);
Log.v(TAG, "onstart_service");
super.onStart(intent, startId);
}
@Override
public void onInit(int status) {
Log.v(TAG, "oninit");
if (status == TextToSpeech.SUCCESS) {
int result = mTts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA ||
result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.v(TAG, "Language is not available.");
} else {
sayHello(str);
}
} else {
Log.v(TAG, "Could not initialize TextToSpeech.");
}
}
private void sayHello(String str) {
mTts.speak(str,
TextToSpeech.QUEUE_FLUSH,
null);
}
}
link|improve this answer
edited Jun 23 at 11:58
answered Jun 23 at 11:52
coastline
133
Was this post useful to you?
更多阅读
转 Android学习笔记(10)————Android的Listview详解1Arr
先感谢该博作者下,本人看了获益匪浅啊!博文来源:http://blog.csdn.net/conowen/article/details/7294230SimpleCursorAdapter方式请看http://blog.csdn.net/conowen/article/details/73065451、Listview简述A view that shows items
英语诗歌:乡村墓园挽歌Elegy Written in a Country Churchyard written in the water
Elegy Written in a Country Churchyard 写在教堂墓地的挽歌Thomas Gray 托马斯·格雷The curfew tolls the knell of parting day,晚钟为告别的白昼敲起了丧钟,The lowing herd wind slowly o'er the lea,咩咩羊群在草地上慢慢盘桓,
in,after,later的用法与区别 after later区别
in,after,later的用法与区别这三个词都有“在……之后”的意义,区别如下:1)in表示从现在开始一段时间以后,它往往和一般将来时肯定句连用。如:I'm coming back in a minute.我一会儿就回来。He will reach here in two days.他两天后将到
日本《豚鼠系列》 重口味吧
日本《豚鼠系列》一共六部:1、Devil’s Experiment 恶魔实验 19852、Flowers Of Flesh And Blood 血肉之花 19853、He Never Dies 他不会死 19864、Mermaid In A Manhole 地窖人鱼 1988(下水道的美人鱼)5、Android Of NotreD
Software-as-a-service软件即服务 assysctrlservice
【Piece1、SaaS的概念 】 SaaS是Software-as-a-service(软件即服务)的简称,是随着互联网技术的发展和应用软件的成熟,而在21世纪开始兴起的一种完全创新的软件应用模式。它与“on-demandsoftware”(按需软件),the application service