我在列表视图中播放mp3歌曲。当我单击“随机播放”按钮时,其突出显示并保持彩色焦点,此刻布尔值变为真。然后,当我再次单击“随机播放”按钮时,突出显示消失了,此刻布尔值应该为假,随机播放应该停止连续播放歌曲。
但是不幸的是,布尔值不会从true变为false,并且始终保持为true。我不明白为什么,因为我没事。主要目的是当第一首歌曲结束时,它应该从第二首开始,然后是第三首。...但是由于布尔值不会返回到其先前的状态(假),即使在我单击“随机播放”按钮之后,它仍会连续播放歌曲。
编辑,这是我为了使视频更易理解而放置的视频
这是我的代码:
public class MainActivity extends AppCompatActivity {
ListView lv;
MediaPlayer mediaPlayer;
private boolean isShuffle = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
btnshuffle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (isShuffle) {
isShuffle = false;
Toast.makeText(getApplicationContext(), "Shuffle is OFF", Toast.LENGTH_SHORT).show();
if(mediaPlayer.isPlaying())
{
mediaPlayer.pause();
buttonPlayPause.setBackgroundResource(R.drawable.ic_play_arrow_black_24dp);
}
else
{
mediaPlayer.start();
buttonPlayPause.setBackgroundResource(R.drawable.ic_pause_black_24dp);
}
SetTimeTotal();
TimeDuration();
btnshuffle.setBackgroundResource(R.drawable.shuffletouch);
oncompletiononce();
} else {
initcommands();
isShuffle = true;
Toast.makeText(getApplicationContext(), "Shuffle is ON", Toast.LENGTH_SHORT).show();
buttonPlayPause.setBackgroundResource(R.drawable.ic_pause_black_24dp);
isRepeat = false;
btnshuffle.setBackgroundResource(R.drawable.shuffletouchfocused);
repeat.setBackgroundResource(R.drawable.btn_repeat);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
nextsong();
UpdateTimeSong();
}
});
buttonPlayPause.setBackgroundResource(R.drawable.ic_pause_black_24dp);
isRepeat = false;
btnshuffle.setBackgroundResource(R.drawable.shuffletouchfocused);
repeat.setBackgroundResource(R.drawable.btn_repeat);
}
}
});
public void nextsong () {
position++;
if(position > arraySong.size()-1)
{
position = 0;
}
if(mediaPlayer.isPlaying())
{
mediaPlayer.pause();
}
CreateMediaPlayer();
//audioPlayer.startPlayer(position);
mediaPlayer.start();
buttonPlayPause.setBackgroundResource(R.drawable.ic_pause_black_24dp);
closebutton.setBackgroundResource(R.drawable.ic_highlight_off_black_24dp);
initcommands();
oncompletiononce();
}
private void UpdateTimeSong()
{
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
//check time song -> if end -> next
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
position++;
if (position > arraySong.size() - 1) {
position = 0;
}
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
CreateMediaPlayer();
mediaPlayer.start();
buttonPlayPause.setBackgroundResource(R.drawable.ic_pause_black_24dp);
SetTimeTotal();
UpdateTimeSong();
}
});
handler.postDelayed(this, 500);
}
},100);
}
}
答案 0 :(得分:0)