【问题标题】:Greet the user on startup using speech在启动时使用语音问候用户
【发布时间】:2016-04-29 14:21:04
【问题描述】:

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

但是当我启动应用程序时,我什么也听不见。

这是我的代码:

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

【问题讨论】:

    标签: android speech-recognition text-to-speech speech-to-text speech


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2012-03-06
      • 2018-11-27
      • 1970-01-01
      • 2016-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-01
      相关资源
      最近更新 更多