Wassauf Khalid
Wassauf Khalid

Reputation: 73

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

i want my app to speak "Hi! My name is Torz. How can I help you ?" whenever someone open the app. I am trying this approach but its not working for me.

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);

}
}

Upvotes: 3

Views: 61

Answers (1)

Abdul Waheed
Abdul Waheed

Reputation: 4678

Add you code

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

In onInit method and it will work.

Upvotes: 2

Related Questions