2010-06-23 5 views
2

J'ai un service d'où j'essaye de démarrer un moteur de TextToSpeech, mais il semble que cela ne fonctionne pas, ainsi est-il possible de commencer des tts d'un service?démarrer le moteur de synthèse vocale à partir d'un service?

Voici ce que j'ai essayé:

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 

} 

} 

Répondre

4

nouvelle TextToSpeech (getApplicationContext(), this);

travaille avec moi ... Mais assurez-vous de laisser le service, jusqu'à ce que la parole se fait ..

+0

« Mais assurez-vous de laisser le service, jusqu'à ce que la parole se fait .. » Est-ce que vous voulez dire que cela fonctionnera si je ne tue pas le service manuellement à travers les paramètres? – pranay

+0

à partir de: http://android-developers.blogspot.com/2009/09/introduction-to-text-to-speech-in.html Lorsque non utilisé ... La fonctionnalité de synthèse vocale repose sur service dédié partagé entre toutes les applications qui utilisent cette fonctionnalité. Lorsque vous avez fini d'utiliser TTS, soyez un bon citoyen et dites-lui «vous n'aurez plus besoin de ses services» en appelant mTts.shutdown(), dans votre méthode Activity onDestroy() par exemple. – arnold

Questions connexes