我试图使用TextToSpeech
该类在我的应用程序中说文本。当我运行我的代码时,我什么也没听到,音量很高。我的代码有什么问题?我需要许可还是什么?
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);
}
}