我从API获取一些MP3文件,并通过mediaPlayer和SeekBar在recyclerView中显示它们。一切正常,但是有两个问题:
我已尽一切可能解决该问题,但我没有。
这是我的ViewHolder代码:
private MediaPlayer mediaPlayer;
private SeekBar seekBar;
private TextView currentTime;
private Timer timer;
private ProgressBar progressBar;
private SimpleDraweeView artistProfile;
private SimpleDraweeView socialNetworkTypeImage;
private TextView artistName;
private TextView postReleaseDate;
private TextView likes;
private String urlMp3;
private View playButtonContainer;
private ImageView playButton;
private ImageView pauseButton;
private int mediaPlayerDuration;
private Handler seekHandler = new Handler();
TelegramVoiceViewHolder(@NonNull View itemView) {
super(itemView);
seekBar = itemView.findViewById(R.id.sb_news_feed_items_telegram_voice_seekbar);
currentTime = itemView.findViewById(R.id.tv_news_feed_items_telegram_voice_time);
progressBar = itemView.findViewById(R.id.iv_news_feed_items_telegram_voice_progress_bar);
artistProfile = itemView.findViewById(R.id.sdv_news_feed_telegram_voice_artist_profile);
socialNetworkTypeImage = itemView.findViewById(R.id.sdv_news_feed_telegram_voice_social_type);
artistName = itemView.findViewById(R.id.tv_news_feed_items_telegram_voice_artist_name);
postReleaseDate = itemView.findViewById(R.id.tv_telegram_voice_page_timer);
likes = itemView.findViewById(R.id.tv_news_feed_items_telegram_voice_post_likes);
playButtonContainer = itemView.findViewById(R.id.rl_news_feed_items_telegram_voice_play_button);
playButton = itemView.findViewById(R.id.iv_news_feed_items_telegram_voice_play_button_image);
pauseButton = itemView.findViewById(R.id.iv_news_feed_items_telegram_voice_pause_button_image);
}
private void onBindViews(DataItem dataItem) {
urlMp3 = dataItem.getAttachment().get(0).getFile();
artistProfile.setImageURI(Uri.parse(dataItem.getUser().getAvatar()));
socialNetworkTypeImage.setImageURI(Uri.parse(TELEGRAM_ICON));
socialNetworkTypeImage.setCropToPadding(true);
socialNetworkTypeImage.setAdjustViewBounds(true);
postReleaseDate.setText(TimeAgo.getTimeAgo(Long.parseLong(dataItem.getDate())));
likes.setText(dataItem.getLikes() + " People Liked This");
artistName.setText(dataItem.getUser().getName());
}
private void setUpMediaPlayer() {
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(context, Uri.parse(urlMp3));
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(mediaPlayer -> {
setUpMediaPlayerElements();
progressBar.setVisibility(View.GONE);
playButton.setVisibility(View.VISIBLE);
timer = new Timer();
timer.schedule(new MainTimer(), 0, 1000);
});
mediaPlayer.setOnCompletionListener(mp -> {
pauseButton.setVisibility(View.GONE);
playButton.setVisibility(View.VISIBLE);
mp.seekTo(0);
});
} catch (IOException e) {
e.printStackTrace();
}
}
private void setUpMediaPlayerElements() {
seekBar.setMax(mediaPlayer.getDuration());
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
mediaPlayer.seekTo(progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
playButtonContainer.setOnClickListener(view -> {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
pauseButton.setVisibility(View.GONE);
playButton.setVisibility(View.VISIBLE);
} else {
mediaPlayer.start();
pauseButton.setVisibility(View.VISIBLE);
playButton.setVisibility(View.GONE);
}
});
}
private String formatDuration(long duration) {
int seconds = (int) (duration / 1000);
int minutes = seconds / 60;
seconds %= 60;
return String.format(Locale.ENGLISH, "%02d", minutes) + ":" + String.format(Locale.ENGLISH, "%02d", seconds);
}
private class MainTimer extends TimerTask {
@Override
public void run() {
((Activity) context).runOnUiThread(() -> {
seekBar.setProgress(mediaPlayer.getCurrentPosition());
mediaPlayer.setOnBufferingUpdateListener((mp, percent) -> seekBar.setSecondaryProgress((percent * mp.getDuration()) / 100));
currentTime.setText(formatDuration(mediaPlayer.getCurrentPosition()));
});
}
}
}
然后我在onBindViewHolder中调用onBindViews方法:
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
DataItem data = dataItems.get(i);
if (data != null) {
((TelegramVoiceViewHolder) viewHolder).onBindViews(data);
((TelegramVoiceViewHolder) viewHolder).setUpMediaPlayer();
break;
}