在我的Android应用程序中,我想要流式传输并播放。我想在单击播放按钮时显示进度条,然后显示暂停按钮。我使用Progressbar是因为在Mediaplayer中播放时会向用户表明发生了一些事情。但是问题是播放按钮显示1秒钟,然后在一小部分时间内显示progrressbar并同时更改暂停按钮。当用户点击paly按钮时如何显示进度条。下面给出了代码
holder.videoPlayVoiceBtn.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
// videoInfo element = (videoInfo) videos.get(position);
// element.setPlayButton((Button) v);
// element.setPlaying(true);
LinearLayout relativeLayout = (LinearLayout) v.getParent();
RelativeLayout layout = (RelativeLayout) relativeLayout
.getParent();
final ProgressBar bar = (ProgressBar) layout
.findViewById(R.id.progressBarVoice);
bar.setVisibility(View.VISIBLE);
SeekBar mSeekBar = (SeekBar) relativeLayout
.findViewWithTag(position);
final SeekBar SeekBar = mSeekBar;
final Button playBtn = (Button) relativeLayout
.findViewWithTag(position + "b");
if (Utilities.message_player != null) {
if (Utilities.message_player.isPlaying()) {
bar.setVisibility(View.GONE);
Utilities.message_player.pause();
holder.username.setKeepScreenOn(false);
videos.get(position).setCurrentDuration(
Utilities.message_player
.getCurrentPosition());
playBtn.setBackgroundResource(R.drawable.play_button_style);
pause_check = true;
} else if (!Utilities.message_player.isPlaying()) {
bar.setVisibility(View.VISIBLE);
playBtn.setVisibility(View.GONE);
Utilities.message_player.seekTo(videos.get(
position).getCurrentDuration());
playBtnPrev
.setBackgroundResource(R.drawable.pause_btn_style);
//
playVoice(videos.get(position)
.getVoice_URL(),
videos.get(position)
.getDuration(),
SeekBar, duration, playBtn,
position, v, 0, bar);
//
Utilities.message_player.start();
pause_check = true;
} else {
bar.setVisibility(View.GONE);
playBtn.setVisibility(View.VISIBLE);
Utilities.message_player.stop();
holder.username.setKeepScreenOn(false);
// Utilities.message_player.reset();
Utilities.message_player = new MediaPlayer();
pause_check = false;
}
playBtnPrev
.setBackgroundResource(R.drawable.play_button_style);
isPlayCheck = true;
} else {
// bar.setVisibility(View.VISIBLE);
// playBtn.setVisibility(View.GONE);
playBtnPrev
.setBackgroundResource(R.drawable.pause_btn_style);
isPlayCheck = false;
}
}
if (pause_check == true) {
} else {
dummy_voice_button = playBtn;
bar.setVisibility(View.VISIBLE);
playBtn.setVisibility(View.GONE);
if (Utilities.message_player == null)
Utilities.message_player = new MediaPlayer();
playVoice(videos.get(position).getVoice_URL(),
videos.get(position).getDuration(),
SeekBar, duration, playBtn, position,
v, 1, bar);
prevPosition = position;
}
}
});
private void playVoice(String url, final int voiceduration,
final SeekBar mSeekBar, final TextView durationtv,
final Button playBtn, final int posToPlay, final View view,
int valInt, final ProgressBar bar) {
if (Utilities.message_player != null) {
Utilities.message_player
.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
Log.e("Play voice", "setOnCompletionListener");
if (play_task != null) {
prevPosition = -1;
playBtn.setBackgroundResource(R.drawable.profile_play_button_style);
durationtv.setText(""
+ videos.get(posToPlay).getDuration());
LinearLayout relativeLayout = (LinearLayout) view
.getParent();
if (((SeekBar) relativeLayout
.findViewWithTag(posToPlay)) != null)
((SeekBar) relativeLayout
.findViewWithTag(posToPlay))
.setProgress(0);
}
}
});
Utilities.message_player
.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
playBtn.setVisibility(View.VISIBLE);
playBtn.setBackgroundResource(R.drawable.pause_btn_style);
bar.setVisibility(View.GONE);
}
});
try {
Log.e("Play voice", "message_player.isnotPlaying()");
Utilities.message_player.reset();
Utilities.message_player.setDataSource(url);
Utilities.message_player.prepare();
Utilities.message_player.start();
holder.username.setKeepScreenOn(true);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}