0

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

}
alizulfuqar
  • 149
  • 1
  • 12

1 Answers1

0
  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.
Shivam
  • 29
  • 11
  • what exactly do you think this -> private boolean isShuffle = false; does? It is assigned before the initCommands. Do you mean in the method? The question is about when isShuffle is true, initCommands isn't even called there. – Stultuske Sep 20 '19 at 06:20
  • @Shivam, before or after initcommands, doesnot have point. the `isshuffle=true` is working, but `isshuffle=false` not working and initcommands dont include important commands. – alizulfuqar Sep 20 '19 at 06:30