Running Android TTS in a Service android tts 中文

Running Android TTS in a Service



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?

  

爱华网本文地址 » http://www.aihuau.com/a/25101012/129810.html

更多阅读

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

声明:《Running Android TTS in a Service android tts 中文》为网友天光微亮分享!如侵犯到您的合法权益请联系我们删除