使用语音在启动时问候用户

Greet the user on startup using speech

我想在用户每次打开应用时用语音问候他们。

但是当我启动应用程序时,我没有听到任何声音。

这是我的代码:

public class MainActivity extends AppCompatActivity {

TextToSpeech textToSpeech;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    textToSpeech =new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if(status==TextToSpeech.SUCCESS)
                textToSpeech.setLanguage(Locale.US);

        }
    });

    textToSpeech.speak("welcome", TextToSpeech.QUEUE_FLUSH, null,null);


}

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

您应该在 TTS 初始化后调用 textToSpeech.speak(),即在调用 onInit() 之后。另请检查 return 值并查看它是否已添加到队列中。