如何在文本转语音处于活动状态时操纵搜索栏和文本视图

时间:2017-03-14 17:10:33

标签: android android-asynctask text-to-speech seekbar

我有一个非常小的例子,我想点击按钮从MainActivity更改为另一个将倒计时然后返回Main的活动(CountDown)。我希望倒计时能够反映在TextView中,同时大声说出来。我还想使用搜索栏来控制每次倒计时之间的延迟。 我希望我得到所有与TTS相关的代码是正确的,即我认为我必须在'onInit'部分中执行'for'循环(或者我将如何确保TTS引擎已初始化并准备好了?)。我也想控制延迟(目前通过搜索栏在'playSilentUtterance'调用中硬编码为'2000',但不确定什么是正确的方式(全局变量?)

这是第二项活动的代码:

public class CountDown extends Activity implements TextToSpeech.OnInitListener,SeekBar.OnSeekBarChangeListener {

    TextView txtCount;
    TextView txtSeekBar;
    SeekBar seekBar;

    private TextToSpeech mTts;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_count_up);

        txtCount = (TextView) findViewById(R.id.txtCount);
        txtSeekBar = (TextView) findViewById(R.id.txtSeekBar);
        seekBar = (SeekBar) findViewById(R.id.seekBar);

        mTts = new TextToSpeech(this, this);
        mTts.setLanguage(Locale.US);
    }

    public void onInit(int i)
    {
        mTts.speak("Starting Countdown", QUEUE_FLUSH, null);

        for(int number=5; number>0; number--){

            mTts.speak(number + "", QUEUE_ADD, null);

            mTts.playSilentUtterance(2000, QUEUE_ADD, null);

            txtCount.setText(number+"");
        }

        Intent intent2Training = new Intent(CountDown.this, MainActivity.class);
        setResult(Activity.RESULT_OK,intent2Training);

        while (mTts.isSpeaking()) {
        }

        finish();
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

        txtSeekBar.setText(seekBar.getProgress() + " / " + seekBar.getMax());
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {}

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {}

}

0 个答案:

没有答案