好的,所以我有一个显示正在播放的歌曲的活动,并且我为横向模式创建了另一个布局,我有一个显示该歌曲的专辑封面的图像视图和操作栏中的标题,所有显示正在播放歌曲的信息,但是当我旋转屏幕然后旋转回纵向模式并用我的下一个按钮切换歌曲然后旋转另一首歌曲时,它仍然显示上一首歌曲的信息。
我的第一个活动(显示所有歌曲)
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);
}
});
我的歌活动课
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();
}
}
如何在旋转时更新专辑封面,它仅在我旋转屏幕时发生,在纵向模式下一切正常,并在我更改歌曲时更新信息。