如何在 public void 方法中连续播放媒体 (mp3)

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

例如,我有 public void 方法 shuffle。我希望当我单击 (onclick) 菜单项时,此方法中的一些媒体 (mp3) 会连续播放,并且在最后一个媒体之后停止播放。我想要 case 个位置从 case 0 连续播放到 case 3 并且在播放 case 3 之后媒体播放器停止播放。我需要它们自动播放,比如 case 0 自动开始 case 1 然后 case 2, case 3。我不知道如何在这个 public 方法

中实现 mediaplayer.setOnCompletionListener
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;     
                }
            }
        });

试试下面的代码:

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

编辑

根据评论中要求的更改

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