每次打开应用时,我都希望用语音向用户致意。
但是当我启动应用程序时,我什么都没听到。
这是我的代码:
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();
}
}
答案 0 :(得分:1)
在初始化TTS之后,即在调用~
之后,您应该调用@IBAction func dateToggle(sender: AnyObject) {
showCell = !showCell
if !showCell {
datePicker.hidden = true
}
tableView.reloadData()
。还请检查返回值,看看它是否已添加到队列中。