Android Studio 中不带文本字段和按钮的文本到语音转换



所以,我想在Android Studio中创建一个不使用文本字段和按钮的文本到语音转换。例如,当我打开应用程序时,它会说"欢迎来到我的应用程序",没有文本字段或任何按钮。我该怎么做?需要你的帮助。

你可以像下面这样做:

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.speech.tts.TextToSpeech;
import java.util.Locale;
public class MainActivity extends Activity {
TextToSpeech t1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1 = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
t1.setLanguage(Locale.ENGLISH);
}
}
});

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
t1.speak("welcome to my app", TextToSpeech.QUEUE_FLUSH, null);
}
}, 100);

}
public void onPause() {
if (t1 != null) {
t1.stop();
t1.shutdown();
}
super.onPause();
}
}

代码是不言自明的,我测试了它并取得了成功的结果。

只需在 onCreate(( 中添加以下内容:

myTTS = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
// replace this Locale with whatever you want                    
Locale localeToUse = new Locale("en","US");
myTTS.setLanguage(localeToUse);
myTTS.speak("Hi, Welcome to my app!", TextToSpeech.QUEUE_FLUSH, null);
}
}
});

相关内容

  • 没有找到相关文章

最新更新