在单击按钮的其他操作之前,ProgressBar未设置为可见

时间:2017-08-23 11:44:00

标签: android android-mediaplayer visibility android-progressbar

我与MediaPlayer合作,希望在调用方法ProgressBar之前显示mediaPlayer.prepare()(可能需要一些时间)。我尝试了AsynkTast, Thread, Handler,但只有在clickPlayButton方法结束时的所有操作之后才显示progressBar。我做错了什么? 谢谢!

ProgressBar声明:

<ProgressBar
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"/>

clickPlayButton方法

public void clickPlayButton(View view) {
    Thread t = new Thread(new Runnable() {
        public void run() {
          h.post(updateProgress);
        }
    });
    t.start();
 //  progressBar.setVisibility(ProgressBar.VISIBLE);
   // new ProgressTask().execute();

    mTimer = new Timer();
    mMyTimerTask = new MyTimerTask();
    if(!mediaPlayer.isPlaying()){
       btnPlay.setImageResource(R.drawable.player_stop);
       try {
            mediaPlayer.setDataSource(DATA_HTTP);
                mediaPlayer.prepare();
           }
       catch (Exception e) { e.printStackTrace(); }

       mediaPlayer.start();
       mTimer.schedule(mMyTimerTask, 10, 5000);
    }
    else {
          btnPlay.setImageResource(R.drawable.player_play);
          mediaPlayer.pause();
          mTimer.cancel();
         }
  //  progressBar.setVisibility(ProgressBar.INVISIBLE);
}

Handler h = new Handler();
Runnable updateProgress = new Runnable() {
    public void run() {
        progressBar.setVisibility(View.VISIBLE);
    }
};

0 个答案:

没有答案