import java.util.Locale;
import android.content.Context;
import android.speech.tts.TextToSpeech;
public class MyTTS implements TextToSpeech.OnInitListener{
private Context context;
private TextToSpeech mTextToSpeech = null;
private boolean isLoaded = false;
// initTTS
public MyTTS(Context context){
try {
this.context = context;
this.mTextToSpeech = new TextToSpeech(context, this);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS)
{
mTextToSpeech.setLanguage(Locale.US);
isLoaded = true;
}
else
{
isLoaded = false;
}
}
// stop action about tts
public void close(){
if(mTextToSpeech != null)
{
mTextToSpeech.stop();
mTextToSpeech.shutdown();
}
}
public void queueSpeak(String text) {
if (isLoaded)
mTextToSpeech.speak(text, TextToSpeech.QUEUE_ADD, null);
}
public void flushSpeak(String text) {
if (isLoaded)
mTextToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
參考資料 : Android TextToSpeech Example
沒有留言:
張貼留言