[英]Text To Speech without textfield and button in Android Studio
So, I want to create a text to speech without the use of textfield and button in Android Studio. 因此,我想在不使用Android Studio中的文本字段和按钮的情况下创建语音文本。 For example when I open the app it will say "WELCOME TO MY APP" without text field or any button.
例如,当我打开应用程序时,它会说“欢迎使用我的应用程序”,而没有文本字段或任何按钮。 How can I do that?
我怎样才能做到这一点? Need your help.
需要你的帮助。
You could do like below: 您可以执行以下操作:
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();
}
}
codes are self explanatory and I tested it with successful result. 代码是不言自明的,我测试成功了。
Just add this in your onCreate(): 只需将其添加到您的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);
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.