使用圆形进度条显示记录进度

时间:2019-01-07 07:19:25

标签: android android-progressbar

我正在开发一个新的Android应用程序,在我的应用程序中,我具有3秒的录音功能。我想显示一个圆形进度条以显示录制进度。录制将在3秒钟后自动停止。这是我的代码,请帮助我添加进度栏。

 private void mStartRecordingAudio() {
        try {
            TemporaryModelCache.stopMediaPlayer();
            TemporaryModelCache.getAsyncInstance().cancel(true);
            TemporaryModelCache.clearAsyncInstance();
            mAudioRecorder = new MediaRecorder();
            mAudioRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
            mAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mAudioRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
            String file_name = mFileToSaveAudio + "recording.3gp";
            mAudioRecorder.setOutputFile(file_name);
            mAudioRecorder.prepare();
            mAudioRecorder.start();
            mImageViewRecord.setImageResource(R.drawable.ic_button_recording);
            circularProgressBar.setVisibility(View.VISIBLE);
            mImagePlayOurs.setImageResource(R.drawable.ic_button_play_ours_disabled);
            mButtonPlayRecordedAudio.setImageResource(R.drawable.button_yours_disabled);
            mViewRecord.setEnabled(false);
            mViewPlayYours.setEnabled(false);
            mViewPlayOurs.setEnabled(false);

        } catch (IllegalStateException ise) {
            Log.e("error", ise.toString());
            // make something ...
        } catch (IOException ioe) {
            Log.e("error", ioe.toString());
            // make something
        }
        new Timer().schedule(new TimerTask() {

            @Override
            public void run() {
                activity.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            mAudioRecorder.stop();
                            mAudioRecorder.reset();
                            mAudioRecorder.release();
                        } catch (RuntimeException stopException) {
                            //handle cleanup here
                        }
                        circularProgressBar.setVisibility(View.GONE);
                        mWowWordActionSounds.mPlayAudioButtonClick();
                        mIsAudioRecorded = true;
                        mButtonPlayRecordedAudio.setImageResource(R.drawable.ic_button_yours_png);
                        mImagePlayOurs.setImageResource(R.drawable.ic_btn_playours);
                        mImageViewRecord.setImageResource(R.drawable.ic_btn_record);
                        mViewPlayYours.setEnabled(true);
                        mViewPlayOurs.setEnabled(true);
                        mViewRecord.setEnabled(true);
                    }
                });

            }
        }, 3000);
}

1 个答案:

答案 0 :(得分:0)

您不会在代码中的任何地方更新进度栏

您应该使用CircularProgressBar.setProgress(circularProgressBar.getProgress()+ 1000);在run()内部。

但是我认为您应该使用Countdown Timer类,而不是使用Timer.schedule()进行调度。

一个简单的CountDownTimer示例,它将每隔1秒在onTick()内部执行一次任务,并在3秒后调用onFinish()

您可以将setProgress的代码放在onTick()中。

new CountDownTimer(30000, 1000) {

 public void onTick(long millisUntilFinished) {
     mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
 }

 public void onFinish() {
     mTextField.setText("done!");
 }
  }.start();

更多信息,请访问:https://developer.android.com/reference/android/os/CountDownTimer