我与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);
}
};