Ok so I have an activity where I display a song that's playing, and I created another layout for landscape mode, I have an imageview that displays the album art from that song and a title in the actionbar, all display information of the playing song, but when I rotate the screen and then rotate back to portrait mode and switch song with my next button and then rotate on that other song, it still displays information of the previous song.
My first activity (displays all songs)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_songs);
recyclerView = findViewById(R.id.recyclerView);
if (recyclerView != null) {
recyclerView.setHasFixedSize(true);
}
songAdapter = new SongAdapter(this, songList);
recyclerView.setAdapter(songAdapter);
songAdapter.notifyDataSetChanged();
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), linearLayoutManager.getOrientation());
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.addItemDecoration(dividerItemDecoration);
//RelativeLayouts
mainLayout = findViewById(R.id.mainLayout);
secondLayout = findViewById(R.id.secondLayout);
currSong = findViewById(R.id.currSong);
//LinearLayouts
songThumbnail = findViewById(R.id.songThumbnail);
mToolbar = findViewById(R.id.mToolbar);
setSupportActionBar(mToolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle(R.string.library);
}
tvCurrSongTitle = findViewById(R.id.tvCurrSongTitle);
tvCurrSongArtist = findViewById(R.id.tvCurrSongArtist);
recyclerView.addOnItemTouchListener(new OnItemClickListeners(this, new OnItemClickListeners.OnItemClickListener() {
@TargetApi(Build.VERSION_CODES.O)
@Override
public void onItemClick(View view, int position) {
songIndex = position;
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(songList.get(songIndex).getData());
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
Toast.makeText(getApplicationContext(), "You Clicked position: " + position + " " + songList.get(position).getData(), Toast.LENGTH_SHORT).show();
tvCurrSongTitle.setText(songList.get(position).getTitle());
tvCurrSongArtist.setText(songList.get(position).getArtist());
} catch (Exception e) {
e.printStackTrace();
}
}
}));
currSong.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent currSong = new Intent(getApplicationContext(), SongActivity.class);
currSong.putExtra("songIndex", songIndex);
startActivity(currSong);
}
});
My Song activity class
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_song);
albumArtLarge = findViewById(R.id.albumArtLarge);
albumArt = findViewById(R.id.albumArt);
mBtnPlayPause = findViewById(R.id.mBtnPlayPause);
mBtnNext = findViewById(R.id.mBtnNext);
mBtnPrev = findViewById(R.id.mBtnPrev);
mBtnShuffle = findViewById(R.id.mBtnShuffle);
mBtnRepeat = findViewById(R.id.mBtnRepeat);
seekBar = findViewById(R.id.seekBar);
tvSongCurrentTime = findViewById(R.id.tvSongCurrentTime);
tvSongTotalTime = findViewById(R.id.tvSongTotalTime);
tvSongListSize = findViewById(R.id.tvSongListSize);
//Listeners
seekBar.setOnSeekBarChangeListener(this);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setOnErrorListener(this);
Intent currSong = getIntent();
b = currSong.getExtras();
mCurrentIndex = (int) b.get("songIndex");
songActivityToolbar = findViewById(R.id.songActivityToolbar);
setSupportActionBar(songActivityToolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle(songList.get(songIndex).getTitle());
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
// Load album art clicked song
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, songList.get(songIndex).getAlbumid());
Picasso.with(this)
.load(albumArtUri)
.error(R.drawable.blackgreygradientbackground)
.into(albumArtLarge);
Picasso.with(this)
.load(albumArtUri)
.error(R.drawable.albumcover)
.resize(500,500)
.into(albumArt);
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
mBtnPlayPause.setImageResource(R.drawable.ic_action_pause_white);
tvSongListSize.setText((mCurrentIndex + 1) + "/" + songList.size());
updateProgressBar();
}
}
mBtnShuffle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isShuffle){
isShuffle = false;
Toast.makeText(getApplicationContext(), "Shuffle is off", Toast.LENGTH_SHORT ).show();
mBtnShuffle.setImageResource(R.drawable.ic_action_shuffle);
}else{
isShuffle = true;
Toast.makeText(getApplicationContext(), "Shuffle is on", Toast.LENGTH_SHORT).show();
mBtnShuffle.setImageResource(R.drawable.ic_shuffle_on);
isRepeat = false;
mBtnRepeat.setImageResource(R.drawable.ic_action_repeat);
}
}
});
mBtnRepeat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isRepeat){
isRepeat = false;
Toast.makeText(getApplicationContext(), "Repeat is off", Toast.LENGTH_SHORT).show();
mBtnRepeat.setImageResource(R.drawable.ic_action_repeat);
}else{
isRepeat = true;
Toast.makeText(getApplicationContext(), "Repeat is on", Toast.LENGTH_SHORT).show();
mBtnRepeat.setImageResource(R.drawable.ic_repeat_on);
isShuffle = false;
mBtnShuffle.setImageResource(R.drawable.ic_action_shuffle);
}
}
});
mBtnPlayPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
mBtnPlayPause.setImageResource(R.drawable.ic_action_play);
} else {
mediaPlayer.start();
mBtnPlayPause.setImageResource(R.drawable.ic_action_pause_white);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
mBtnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nextSong();
}
});
mBtnPrev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
prevSong();
}
});
}
private void playSongNumber(final int index) {
try{
mediaPlayer.reset();
mediaPlayer.setDataSource(songList.get(index).getData());
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle(songList.get(index).getTitle());
}
//Load album art
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, songList.get(index).getAlbumid());
Picasso.with(getApplicationContext())
.load(albumArtUri)
.error(R.drawable.blackgreygradientbackground)
.into(albumArtLarge);
Picasso.with(getApplicationContext())
.load(albumArtUri)
.error(R.drawable.albumcover)
.resize(500,500)
.into(albumArt);
updateProgressBar();
}
});
seekBar.setProgress(0);
seekBar.setMax(100);
}catch (Exception e){
e.printStackTrace();
}
}
private void nextSong(){
mCurrentIndex++;
mCurrentIndex %= songList.size();
playSongNumber(mCurrentIndex);
mBtnPlayPause.setImageResource(R.drawable.ic_action_pause_white);
tvSongListSize.setText((mCurrentIndex + 1) + "/" + songList.size());
Toast.makeText(getApplicationContext(), "You Clicked position: " + mCurrentIndex + " " + songList.get(mCurrentIndex).getData(), Toast.LENGTH_SHORT).show();
}
private void prevSong(){
mCurrentIndex = mCurrentIndex > 0 ? mCurrentIndex - 1 : songList.size() - 1;
playSongNumber(mCurrentIndex);
mBtnPlayPause.setImageResource(R.drawable.ic_action_pause_white);
tvSongListSize.setText((mCurrentIndex + 1) + "/" + songList.size());
Toast.makeText(getApplicationContext(), "You Clicked position: " + mCurrentIndex + " " + songList.get(mCurrentIndex).getData(), Toast.LENGTH_SHORT).show();
}
@Override
public void onCompletion(MediaPlayer mp) {
if (isRepeat){
playSongNumber(mCurrentIndex);
}else if(isShuffle){
Random random = new Random();
mCurrentIndex = random.nextInt((songList.size() - 1) + 1);
tvSongListSize.setText((mCurrentIndex + 1) + "/" + songList.size());
playSongNumber(mCurrentIndex);
}else if (mCurrentIndex < songList.size()-1){
mediaPlayer.reset();
nextSong();
tvSongListSize.setText((mCurrentIndex + 1) + "/" + songList.size());
}else{
playSongNumber(0);
tvSongListSize.setText((1) + "/" + songList.size());
}
}
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Toast.makeText(getApplicationContext(), "what:" + what + "extra:" +extra , Toast.LENGTH_SHORT).show();
return true;
}
private Runnable mUpdateTime = new Runnable() {
@Override
public void run() {
long songCurrentTime = mediaPlayer.getCurrentPosition();
long songTotalTime = mediaPlayer.getDuration();
tvSongCurrentTime.setText(""+utilities.msToTimer(songCurrentTime));
tvSongTotalTime.setText(""+utilities.msToTimer(songTotalTime));
int progress = (int)(utilities.getProgressPercentage(songCurrentTime, songTotalTime));
seekBar.setProgress(progress);
mHandler.postDelayed(this, 100);
}
};
public void updateProgressBar(){
mHandler.postDelayed(mUpdateTime, 100);
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
mHandler.removeCallbacks(mUpdateTime);
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
mHandler.removeCallbacks(mUpdateTime);
int songTotalTime = mediaPlayer.getDuration() ;
int currentPosition = utilities.progressToTimer(seekBar.getProgress(), songTotalTime);
mediaPlayer.seekTo(currentPosition);
updateProgressBar();
}
}
How can I update the album art on rotation, it only occurs when I rotate the screen, in portrait mode it all works fine and updates the information when I change the song.