7
votes

Courir Android TTS dans un service

J'essaie d'obtenir la TTS de Android à courir à l'intérieur d'un service, mais je ne sais pas pourquoi cela ne fonctionne pas, il compile, mais cela ne fonctionne pas.

Le toast Notification fonctionnent cependant. P>

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);
}

}


2 commentaires

Veuillez décrire plus en détail ce que vous entendez par "ne fonctionne pas". Que voyez-vous sur logcat?


Débogou / texttostostSpececeta (512): TextTostSpecreta non mis à jour - DFAUING à la plate-forme de base TextToStoSoSech pour la parole Verbose / TtsSice (418): TTS Service reçu info / TtsService (418): Arrêter Info / TtsService (418): Arrêté Verbose / Ttsservice (418) : TTS Processing: Verbose / TtsSservice (418): Ttsservice.setlanguage (Eng, USA,) Info / Svox Pico Engine (418): Langue déjà chargée (EN-US == EN-US) Débogou / DALVIKVM (68): GC_CONCURENT Freed 719k, 48% gratuit 4093K / 7751K, externe 2041K / 2378K, pause de 21ms + 27ms debug / SNTPClient (68): Demande de demande Échec de la demande: Java.net.socketException: Adresse Famille non prise en charge par protocole


3 Réponses :


1
votes

Eh bien, faire cette question m'a donné une meilleure réponse que je puisse trouver avec Google.

Allez à la réponse à cette question et changez-la pour correspondre à votre application.

TTS ne parle pas d'un service alors qu'il le fait d'une activité dans Android


0 commentaires

16
votes

Vous pouvez faire comme ci-dessous: ça marche pour moi. Vous devez créer une activité pour démarrer ce service, comme ceci: this.startservice (intention) xxx


0 commentaires

6
votes

https://developer.android.com/reference/andrroid/ Discours / TTS / TextTostStSpectreService.html

Depuis l'API Niveau 14, Android a ajouté une classe de service TextStoStoShech par défaut qui fait ce que vous voulez.


0 commentaires