简体   繁体   中英

Boolean does not returning from true to false

I have mp3 songs playing in listview. When I click shuffle button, its highlighting and stay focused colorful and this moment boolean became true. Then when I click again shuffle button, the highlight gone and this moment boolean should be false and shuffle should stop to play songs continiously.

But unfortunately, the boolean does not back from true to false and it stay always true. I cant understand why, because I do all right. The main purpose is when first song finish, it should start second, then third.... But because of boolean does not return to its previous state (false), even after I click shuffle button, its still plays songs continiously.

EDIT Here is i put video for to be more understandable

https://streamable.com/x9ll9

This is my codes:

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);
    }

}
  1. Try assigning isShuffle before initcommands().
  2. If 1st option did not work, try assigning isShuffle as the 1st line in onClick() and then perform operation based on isShuffle state.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM