我試圖使用TextToSpeech
類來說我的應用程序中的文本。當我運行我的代碼時,我什麼都沒聽到,音量很高。我的代碼有什麼問題?我需要許可或任何東西嗎?Android文本到語音
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);
}
}