2012-07-10 10 views
7

Sto sviluppando un'applicazione Android in cui utilizzo la conversione da testo a voce. Quello di cui ho bisogno quando apro la mia applicazione esegue la conversione da testo a parlato. Dopo il completamento di questo che voglio fare un certo codice thing.My sembraImpossibile rilevare il completamento di TTS (callback) Android.

public class Mainactivity extends Activity implements OnInitListener, OnUtteranceCompletedListener{ 
    private static int REQ_CODE = 1; 
    private TextToSpeech tts = null; 
    private boolean ttsIsInit = false; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    startTextToSpeech(); 
    } 

    private void startTextToSpeech() { 
     Intent intent = new Intent(Engine.ACTION_CHECK_TTS_DATA); 
     startActivityForResult(intent, REQ_CODE); 
    } 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == REQ_CODE) { 
      if (resultCode == Engine.CHECK_VOICE_DATA_PASS) { 
       tts = new TextToSpeech(this, this); 
      } 
      else { 
       Intent installVoice = new Intent(Engine.ACTION_INSTALL_TTS_DATA); 
       startActivity(installVoice); 
      } 
     } 
    } 

     public void onInit(int status) { 
      if (status == TextToSpeech.SUCCESS) { 
       ttsIsInit = true; 
       int result = tts.setOnUtteranceCompletedListener(this); 
       if (tts.isLanguageAvailable(Locale.ENGLISH) >= 0) 
        tts.setLanguage(Locale.ENGLISH); 
       tts.setPitch(5.0f); 
       tts.setSpeechRate(1.0f); 

       HashMap<String, String> myHashAlarm = new HashMap<String, String>(); 
        myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_ALARM)); 
        myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "SOME MESSAGE"); 
        tts.speak("hi how are you?", TextToSpeech.QUEUE_FLUSH, myHashAlarm); 
      } 
     } 

    @Override 
    public void onDestroy() { 
     if (tts != null) { 
     tts.stop(); 
     tts.shutdown(); 
     } 
     super.onDestroy(); 
    } 

    @Override 
    public void onUtteranceCompleted(String uttId) { 
     Toast.makeText(Mainactivity.this,"done", Toast.LENGTH_LONG).show(); 
     if (uttId.equalsIgnoreCase("done")) { 
      Toast.makeText(Mainactivity.this,"inside done", Toast.LENGTH_LONG).show(); 
     } 
    } 
} 

Quando apro il mio testo applicazione to speech lavorando bene. Ma come rilevare se il text to speech è stato completato o no. Aiuto n. ..... Grazie .....

risposta

7

Se si utilizza il livello API 15 o successivo, è possibile impostare un listener di avanzamento sul riferimento TextToSpeech utilizzando

setOnUtteranceProgressListener(UtteranceProgressListener listener) 

Riceverete callback che segnalano l'avanzamento del TTS, inclusa una richiamata al termine. Vedere http://developer.android.com/reference/android/speech/tts/TextToSpeech.html e http://developer.android.com/reference/android/speech/tts/UtteranceProgressListener.html

Tuttavia, noto che si sta già utilizzando il deprecato OnUtteranceCompletedListener. Ricevi i callback su onUtteranceCompleted()? Anche questo dovrebbe funzionare.

+0

Hi David Ho provato a soluzione, ma mi dà errore per setOnUtteranceProgressListener.And per onUtteranceCompleted esso si esegue senza errori ma non dà il messaggio in onUtteranceCompleted, quindi c'è un modo per risolvere questo problema. Se sto facendo qualcosa di sbagliato.Aiuto Aiuto ... – nilkash

+1

Si chiama 'int result = tts.setOnUtteranceCompletedListener (thi s);' per impostare il listener. Controlla il valore della variabile 'result'. SUCCESSO (0) o ERRORE (-1) dovrebbero tornare. Inoltre, aggiungi un po 'di logging a 'onUtteranceCompleted()' per vedere se effettivamente ci arriva. Controlla anche il tuo logcat per eventuali altri strani errori. –

+0

Grazie, grazie David per la risposta. Funziona bene. Uso la registrazione invece di toast e funziona bene. Grazie... – nilkash

3

Ecco qualche codice da here che ti aiuta a essere compatibile in modo da non dover indirizzare 15.

private void setTtsListener() 
    { 
     final SpeechRecognizingAndSpeakingActivity callWithResult = this; 
     if (Build.VERSION.SDK_INT >= 15) 
     { 
      int listenerResult = tts.setOnUtteranceProgressListener(new UtteranceProgressListener() 
      { 
       @Override 
       public void onDone(String utteranceId) 
       { 
        callWithResult.onDone(utteranceId); 
       } 

       @Override 
       public void onError(String utteranceId) 
       { 
        callWithResult.onError(utteranceId); 
       } 

       @Override 
       public void onStart(String utteranceId) 
       { 
        callWithResult.onStart(utteranceId); 
       } 
      }); 
      if (listenerResult != TextToSpeech.SUCCESS) 
      { 
       Log.e(TAG, "failed to add utterance progress listener"); 
      } 
     } 
     else 
     { 
      int listenerResult = tts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() 
      { 
       @Override 
       public void onUtteranceCompleted(String utteranceId) 
       { 
        callWithResult.onDone(utteranceId); 
       } 
      }); 
      if (listenerResult != TextToSpeech.SUCCESS) 
      { 
       Log.e(TAG, "failed to add utterance completed listener"); 
      } 
     } 
    } 
7

Chiamare il setOnUtteranceCompletedListener all'interno della funzione onInit dell'oggetto tts.

Se si desidera apportare modifiche all'interfaccia utente alla chiamata della funzione onUtteranceCompleted, aggiungere il codice all'interno di un metodo runOnUIThread.

E ricordatevi di aggiungere il valore HashMap param mentre chiamando la funzione parlare()

Esempio:

TextToSpeech tts= new TextToSpeech(context, new OnInitListener() { 

@Override 
public void onInit(int status) { 

    mTts.setOnUtteranceCompletedListener(new OnUtteranceCompletedListener() { 

     @Override 
     public void onUtteranceCompleted(String utteranceId) { 

      runOnUiThread(new Runnable() { 

       @Override 
       public void run() { 
       //UI changes 
       } 
      }); 
     } 
    }); 

} 
}); 


HashMap<String, String> params = new HashMap<String, String>(); 

params.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"stringId"); 

tts.speak("Text to Speak",TextToSpeech.QUEUE_FLUSH, params); 
Problemi correlati