Im trying to use the TextToSpeech
class to say text in my app. When I run my code I don't hear anything, the volume is high. What is wrong with my code? Do I need a permission or anything?
public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener {
TextToSpeech textToSpeech;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textToSpeech = new TextToSpeech(this, this);
speakOut();
}
@Override
public void onInit(int Text2SpeechCurrentStatus) {
if (Text2SpeechCurrentStatus == TextToSpeech.SUCCESS) {
int result = textToSpeech.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
speakOut();
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
private void speakOut() {
String g= "Hello";
textToSpeech.speak(g, TextToSpeech.QUEUE_FLUSH, null);
}
}