Я получаю некоторые MP3-файлы из API и показываю их в программе recyclerView от mediaPlayer и SeekBar. Все отлично работает но есть 2 проблемы:
- MediaPlayer.setOnCompletionListener Не работает.
- Панель поиска вообще не работает, у нее нет функциональности, она не двигается, а seekBar.setOnSeekBarChangeListener не работает
Работа. Я использовал класс Timer и мой TextView, который должен
чтобы показать текущее время работы медиаплеера в таймере,
но seekBar не делает!
Я использовал все возможности, чтобы решить проблему, но не смог.
Вот мой код для 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()));
});
}
}
}
и затем я вызываю метод onBindViews в onBindViewHolder:
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;
}