Sto cercando di ottenere l'API TTS di Android per leggere il mio "enunciato" e quindi chiamare il listener onUtteranceCompleted() senza successo. Ho registrato il mio oggetto TTS e restituisce SUCCESSO, quindi non riesco a capire perché la mia callback non viene chiamata.Android TTS onUtterance Il callback completato non viene chiamato
Ho provato a cercare aiuto, ma sembra che anche altri abbiano difficoltà con questo. Mi sto perdendo qualcosa di semplice?
Grazie per l'aiuto che puoi offrire.
package com.test.mytts;
import java.util.HashMap;
import android.app.Activity;
import android.media.AudioManager;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.speech.tts.TextToSpeech.OnUtteranceCompletedListener;
import android.widget.TextView;
import android.widget.Toast;
public class MyTTS extends Activity implements OnInitListener, OnUtteranceCompletedListener
{
TextView tv;
private TextToSpeech _tts;
@Override
public void onCreate(Bundle savedInstanceState)
{
tv = new TextView(this);
tv.setText("MyTTS: ");
super.onCreate(savedInstanceState);
setContentView(tv);
_tts = new TextToSpeech(this, this);
}
@Override
public void onInit(int status)
{
HashMap<String, String> myHashAlarm = new HashMap<String, String>();
myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_NOTIFICATION));
myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "test");
if (status == TextToSpeech.SUCCESS)
{
Toast.makeText(this, "Trying to speak...", Toast.LENGTH_SHORT).show();
int result = _tts.setOnUtteranceCompletedListener(this);
tv.append(String.valueOf(result));
_tts.setSpeechRate((float) .5);
_tts.speak("Testing one, two, three", TextToSpeech.QUEUE_ADD, myHashAlarm);
}
else
Toast.makeText(this, "Failed to initialize TTS.", Toast.LENGTH_SHORT).show();
}
@Override
public void onUtteranceCompleted(String utteranceId)
{
Toast.makeText(this, "onUtteranceCompleted", Toast.LENGTH_SHORT).show();
}
@Override
public void onDestroy()
{
super.onDestroy();
_tts.shutdown();
}
}
Non vedo nulla di sbagliato in quello che hai, ma probabilmente stai meglio usando le istruzioni di Log piuttosto che bruscamente per tenere traccia di ciò che sta succedendo. Quindi è possibile rivedere l'ordine in logcat. –
Penso che questo vi aiuterà a: [http://stackoverflow.com/questions/4658376/how-to-know-when-tts-is-finished][1] [1]: http: //stackoverflow.com/questions/4658376/how-to-know-when-tts-is-finished – uriellabs