Search code examples
javaandroidandroid-mediaplayershuffle

How to play media (mp3) consecutively inside public void method


Example, i have public void method shuffle. i want when i click (onclick) menuitem, some media (mp3) inside this method play consecutively and after last media it stops to play. i want case positions play consecutively from case 0 till case 3 and after play case 3 the mediaplayer stops to play. i need they play automatically, like after case 0 automatically starts case 1 then case 2, case 3. i dont know how to implement mediaplayer.setOnCompletionListener inside this public method

public class MainActivity extends AppCompatActivity{

ListView lv;
MediaPlayer mediaPlayer;
int position = 0;

}


@Override
    protected void onCreate(Bundle savedInstanceState) {

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
            position = pos;
           .....
           .....

}

public void shuffle1(MenuItem item){
        mediaPlayer.stop();

        if(position > 3){
            position = 0;

        }
        else if(position < 0){
            position = 3;
        }

        switch(position){

            case 0:
                mediaPlayer  = MediaPlayer.create(this, R.raw.al098);
                break;
            case 1:
                mediaPlayer  = MediaPlayer.create(this, R.raw.al099);
                break;
            case 2:
                mediaPlayer  = MediaPlayer.create(this, R.raw.al100);
                break;
            case 3:
                mediaPlayer  = MediaPlayer.create(this, R.raw.al101);
                break;
        }

        mediaPlayer.start();
}

EDIT: I did like this, but still no affect:


    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            public void onCompletion(MediaPlayer mp) {
                if(position == 0){
                    position++;
                    mediaplayer.start();
                }else if (position ==1){
                    position++;
                    mediaplayer.start();
                }else if (position ==2){
                    position++;
                    mediaplayer.start();
                }else if (position ==3){
                    position++;
                    mediaplayer.start();
                }else if (position ==4){
                    mediaplayer.stop();
                return;     
                }
            }
        });


Solution

  • try below code:

    public void shuffle1(MenuItem item){
            mediaPlayer.stop();
    
            switch(position){
    
                case 0:
                    mediaPlayer  = MediaPlayer.create(this, R.raw.al098);
                    break;
                case 1:
                    mediaPlayer  = MediaPlayer.create(this, R.raw.al099);
                    break;
                case 2:
                    mediaPlayer  = MediaPlayer.create(this, R.raw.al100);
                    break;
                case 3:
                    mediaPlayer  = MediaPlayer.create(this, R.raw.al101);
                    break;
            }
            position++;
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                public void onCompletion(MediaPlayer mp) {
                    if(position == 4){
                        mediaPlayer.stop();
                        return;
                    } else {
                        shuffle1(item);
                    }
                }
            });
    
            mediaPlayer.start();
    }
    

    EDIT

    As per changes asked in the comments

    public void shuffle1(final MenuItem item, int startPosition, boolean playNow){
            mediaPlayer.stop();
    
            int position = this.position;
            if(playNow){
                position = startPosition;
            }
            switch(position){
    
                case 0:
                    mediaPlayer  = MediaPlayer.create(this, R.raw.al098);
                    break;
                case 1:
                    mediaPlayer  = MediaPlayer.create(this, R.raw.al099);
                    break;
                case 2:
                    mediaPlayer  = MediaPlayer.create(this, R.raw.al100);
                    break;
                case 3:
                    mediaPlayer  = MediaPlayer.create(this, R.raw.al101);
                    break;
            }
            if(!playNow){
                this.position++;
            }        
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                public void onCompletion(MediaPlayer mp) {
                    if(this.position == 4){
                        mediaPlayer.stop();
                        return;
                    } else {
                        shuffle1(item, this.position, false);
                    }
                }
            });
    
            mediaPlayer.start();
    }