我正在尝试制作一个文本到语音应用程序,我的所有代码都很好。我想在它完成讲话后执行一些代码。我不知道该写什么以及在哪里写该代码。我尝试找到许多解决方案,但它们对我不起作用。请帮帮我。提前致谢。
public class Main extends AppCompatActivity {
private TextToSpeech tts;
private EditText et;
private SeekBar sb_pitch;
private SeekBar sb_speed;
private Button bt;
private Button bt2;
private Button bt3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bt = findViewById(R.id.bt);
bt2 = findViewById(R.id.bt2);
bt3 = findViewById(R.id.bt3);
tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(status == TextToSpeech.SUCCESS)
{
tts.setLanguage(Locale.ENGLISH);
bt.setEnabled(true);
}
}
});
et = findViewById(R.id.et);
sb_pitch = findViewById(R.id.sb_pitch);
sb_speed = findViewById(R.id.sb_speed);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(et.getText().toString().equals(""))
{
Toast.makeText(getApplicationContext(),"No Text Detected!",Toast.LENGTH_SHORT).show();
}
else
{
bt.setEnabled(false);
sb_pitch.setEnabled(false);
sb_speed.setEnabled(false);
speak();
}
}
});
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tts.stop();
bt.setEnabled(true);
sb_pitch.setEnabled(true);
sb_speed.setEnabled(true);
}
});
bt3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (tts.isSpeaking())
{
et.setText("");
bt.setEnabled(false);
sb_pitch.setEnabled(false);
sb_speed.setEnabled(false);
}
else {
et.setText("");
bt.setEnabled(true);
sb_pitch.setEnabled(true);
sb_speed.setEnabled(true);
sb_pitch.setProgress(50);
sb_speed.setProgress(50);
}
}
});
}
private void speak()
{
String text = et.getText().toString();
float pitch = (float)sb_pitch.getProgress() / 50;
if(pitch<0.1)pitch = 0.1f;
float speed = (float)sb_speed.getProgress() / 50;
if(speed<0.1)speed = 0.1f;
tts.setPitch(pitch);
tts.setSpeechRate(speed);
tts.speak(text,TextToSpeech.QUEUE_FLUSH,null);
}
@Override
protected void onDestroy() {
if(tts!=null)
{
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
}
任何帮助,将不胜感激 :)