Код: Выделить всё
public void onItemClick(int position) {
RV_Item_Song clickedSong = displayedSongList.get(position);
// new song selected
currentlyPlayingSong = clickedSong.getTitleInfo();
//Extract and format the song title and interpret from the RV item
String interpret = clickedSong.getInterpretInfo();
int index = Math.min(interpret.indexOf(','), interpret.indexOf(';'));
if (index == -1) {
index = Math.max(interpret.indexOf(','), interpret.indexOf(';'));
}
currentlyPlayingFirstInterpret = (index == -1) ? interpret : interpret.substring(0, index);
bitmapOfTheCurrentSong = extractAlbumArt(clickedSong.getFilePath());
Map song = MainActivity.sqLite_DB
.getSongByTitleAndInterpret(currentlyPlayingSong, currentlyPlayingFirstInterpret);
String fileName = song.get("file_name");
String filePath = Environment.getExternalStorageDirectory().getPath() + "/Music/" + fileName;
File file = new File(filePath);
if (!file.exists()) {
Toast.makeText(getContext(), "File not found", Toast.LENGTH_SHORT).show();
return;
}
try {
Log.e("NextSongTag", "FR_RV_Songs - onItemClick - try new song - currentlyPlayingSong: " + currentlyPlayingSong);
playSong(filePath);
setCompletionListener();
songsAdapter.setCurrentlyPlayingPosition(position);
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getContext(), "Error playing file", Toast.LENGTH_SHORT).show();
}
}
Код: Выделить всё
private void setCompletionListener() {
Log.d("NextSongTag", "FR_RV_Song - setCompletionListener - method call " );
mediaPlayer.setOnCompletionListener(mp -> {
Log.d("NextSongTag", "FR_RV_Song - setCompletionListener - setOnCompletionListener called " );
songIsPlaying = false;
if (MainActivity.isRepeatModeActive) {
mp.seekTo(0);
mp.start();
songIsPlaying = true;
return;
}
// auto-advance
int nextPos = findNextSongPosition();
if (nextPos != -1) {
RV_Item_Song clickedSong = displayedSongList.get(songsAdapter.getCurrentlyPlayingPosition());
Log.d("NextSongTag", "FR_RV_Song - setCompletionListener - clickedSong: "+ clickedSong.getTitleInfo() );
clickedSong.setTitleInfo("Test2");
Log.d("NextSongTag", "FR_RV_Song - setCompletionListener - clickedSong: "+ clickedSong.getTitleInfo() );
Log.d("NextSongTag", "FR_RV_Song - setCompletionListener - nextPosition called: " + nextPos );
onItemClick(nextPos);
}
});
}
Код: Выделить всё
public void setCurrentlyPlayingPosition(int position) {
// Notify the adapter that the data has changed
int oldPosition = currentlyPlayingPosition;
currentlyPlayingPosition = position;
if (oldPosition != -1) notifyItemChanged(oldPosition, null);
if (currentlyPlayingPosition != -1) notifyItemChanged(currentlyPlayingPosition, null);
notifyDataSetChanged();
Log.d("NextSongTag", "RV_Adapter setCurrentPlayingPosition - getItemCount(): " + getItemCount());
Log.d("NextSongTag", "RV_Adapter setCurrentPlayingPosition - oldPosition: " + oldPosition);
Log.d("NextSongTag", "RV_Adapter setCurrentPlayingPosition - currentlyPlayingPosition: " + currentlyPlayingPosition);
}
< /code>
На самом деле все работает нормально: когда песня нажимается, она воспроизводится. Когда песня закончена, используется метод MediaPlayer.SetOnCompletionListener (mp -> {
Вот выход из LogCat после MediaPlayer.setOncompletionListener (mp -> { называется:
Код: Выделить всё
2025-09-02 14:38:22.607 17232-17232 NextSongTag com.example.mp3playlist D FR_RV_Song - setCompletionListener - setOnCompletionListener called
2025-09-02 14:38:22.617 17232-17232 NextSongTag com.example.mp3playlist D RV_Adapter getCurrentPlayingPosition - getCurrentlyPlayingPosition: 9
2025-09-02 14:38:22.617 17232-17232 NextSongTag com.example.mp3playlist D FR_RV_Song - setCompletionListener - clickedSong: 21 Questions
2025-09-02 14:38:22.617 17232-17232 NextSongTag com.example.mp3playlist D FR_RV_Song - setCompletionListener - clickedSong: Test2
2025-09-02 14:38:22.617 17232-17232 NextSongTag com.example.mp3playlist D FR_RV_Song - setCompletionListener - nextPosition called: 10
2025-09-02 14:38:22.672 17232-17232 NextSongTag com.example.mp3playlist E FR_RV_Songs - onItemClick - try new song - currentlyPlayingSong: Candy Shop
2025-09-02 14:38:22.741 17232-17232 NextSongTag com.example.mp3playlist D Now playing: /storage/emulated/0/Music/50 Cent - Candy Shop.mp3
2025-09-02 14:38:22.742 17232-17232 NextSongTag com.example.mp3playlist D FR_RV_Song - setCompletionListener - method call
2025-09-02 14:38:22.742 17232-17232 NextSongTag com.example.mp3playlist D RV_Adapter setCurrentPlayingPosition - getItemCount(): 1748
2025-09-02 14:38:22.742 17232-17232 NextSongTag com.example.mp3playlist D RV_Adapter setCurrentPlayingPosition - oldPosition: 9
2025-09-02 14:38:22.742 17232-17232 NextSongTag com.example.mp3playlist D RV_Adapter setCurrentPlayingPosition - currentlyPlayingPosition: 10
Почему метод onbindviewhiewholder не вызывается, когда назовен обновляется в настоящее время, когда я поднимается в соответствии с навыками. OncompletionListener Fires, а не только когда я снова перейду к этому фрагменту.
Подробнее здесь: https://stackoverflow.com/questions/797 ... riggered-v