我们怎样才能让我们的应用程序在打开应用程序时说出文字?

how can we make our app to speak text on opening the app?

我希望我的应用程序在有人打开应用程序时说话 "Hi! My name is Torz. How can I help you ?"。我正在尝试这种方法,但它对我不起作用。

public class MainActivity extends AppCompatActivity {

TextToSpeech t1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    t1=new TextToSpeech(MainActivity.this, new TextToSpeech.OnInitListener() 
  {
        @Override
        public void onInit(int status) {
            if(status != TextToSpeech.ERROR) {
                t1.setLanguage(Locale.ENGLISH);
            }
        }
    });
    String b="Hi! My name is Torz. How can I help you ?";
    t1.speak(b,TextToSpeech.QUEUE_FLUSH,null);

}
}

添加你的代码

 String b="Hi! My name is Torz. How can I help you ?";
 t1.speak(b,TextToSpeech.QUEUE_FLUSH,null);

在 onInit 方法中,它将起作用。