Skip to content
Advertisement

Playing Multiple Mediaplayers with OnCompletionListener in Android Studio

help me, I’m a beginner, I’m trying to make an application in which there is audio that needs to be played, I try to use MediaPlayer, so that the audio can be played and paused, I have implemented these 3 mediaplayer with setOnCompletion and array methods when calling the audio folder,like this:

    mediaPlayer.setOnCompletionListener(completionListener);
    MediaPlayer.OnCompletionListener completionListener = new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            play++;
            if (play < playList.length) {
                mediaPlayer = MediaPlayer.create(BackGroundSound.this, playList[play]);
                mediaPlayer.start();
            } else {
                play = 0;
                mediaPlayer = MediaPlayer.create(BackGroundSound.this, playList[play]);
                mediaPlayer.start();
            }
        }
    };

and this array for file MediaPlayer:

int[] playList = new int[3];
playList[0] = R.raw.madtarqiqc1;
playList[1] = R.raw.madtarqiqc2;
playList[2] = R.raw.madtarqiqc3;

but when I clicked button, the program that I made does not work, the medialayer and button no response, what should I add, I have redesigned this code many times but the results remain the same, is there a code or method that was missed?

This is for the full source code that I have:

   public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    MediaPlayer mediaPlayer;
    ImageButton btn1, btn2, btn3;
    int[] playList;
    int play = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn1 = (ImageButton) findViewById(R.id.play_toggle_ratarqiq1);
        btn2 = (ImageButton) findViewById(R.id.play_toggle_ratarqiq2);
        btn3 = (ImageButton) findViewById(R.id.play_toggle_ratarqiq3);

        int[] playList = new int[3];
        playList[0] = R.raw.madtarqiqc1;
        playList[1] = R.raw.madtarqiqc2;
        playList[2] = R.raw.madtarqiqc3;

        mediaPlayer = MediaPlayer.create(this, playList[play]);
        mediaPlayer.setOnCompletionListener(completionListener);

    }

    MediaPlayer.OnCompletionListener completionListener = new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            play++;
            if (play < playList.length) {
                mediaPlayer = MediaPlayer.create(MainActivity.this, playList[play]);
                mediaPlayer.start();
            } else {
                play = 0;
                mediaPlayer = MediaPlayer.create(MainActivity.this, playList[play]);
                mediaPlayer.start();
            }
        }
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.play_toggle_ratarqiq1:
                playMusic(0);
                break;
            case R.id.play_toggle_ratarqiq2:
                playMusic(1);
                break;
            case R.id.play_toggle_ratarqiq3:
                playMusic(2);
                break;
        }
    }

    private void playMusic(int position) {
        mediaPlayer.stop();
        mediaPlayer = MediaPlayer.create(this, playList[position]);
        mediaPlayer.start();

        changeView(position);
    }

    private void changeView(int position) {
        if (position == 0) {
            btn1.setImageResource(R.drawable.ic_play_circle_outline_black_24dp);
            btn2.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
            btn3.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
        } else if (position == 1) {
            btn1.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
            btn2.setImageResource(R.drawable.ic_play_circle_outline_black_24dp);
            btn3.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
        } else if (position == 2) {
            btn1.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
            btn2.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
            btn3.setImageResource(R.drawable.ic_play_circle_outline_black_24dp);
        }
    }

}

Advertisement

Answer

There are few crucial things that you missed :

  • to set onClickListner() in those Button objects
  • to reuse the MediaPlayer instance(you created new one everytime you need)
  • to properly update the play variable which I believe is the index of currently played song

I tried to make it fully readable as I can. Here is the code for MainActivity:

    public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private MediaPlayer mediaPlayer;
    private ImageButton btn1, btn2, btn3;
    private int[] playList;
    // initializing it to -1 so that it is out of bounds of the array playList
    private int play = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn1 = findViewById(R.id.play_toggle_ratarqiq1);
        btn2 = findViewById(R.id.play_toggle_ratarqiq2);
        btn3 = findViewById(R.id.play_toggle_ratarqiq3);

        // you forgot to setonclicklistener in these buttons thats why they were not reponding
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);

        playList = new int[3];
        playList[0] = R.raw.madtarqiqc1;
        playList[1] = R.raw.madtarqiqc2;
        playList[2] = R.raw.madtarqiqc3;
        // use constructor to create a mediaplayer object rather than this static create method
        // mediaPlayer = MediaPlayer.create(this, playList[play]);
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setOnCompletionListener(completionListener);

    }

    MediaPlayer.OnCompletionListener completionListener = new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            play++;
            if (play < playList.length) {
                // we will not assign mediaPlayer to new instance instead we will only change data source
                // and reuse the single instance everywhere in this activity
                // mediaPlayer = MediaPlayer.create(MainActivity.this, playList[play]);
                try {
                    mediaPlayer.setDataSource(MainActivity.this, Uri.parse("android.resource://" + getPackageName() + "/res/raw/"
                            + getResources().getResourceName(playList[play])));
                    mediaPlayer.start();
                } catch (IOException e) {
                    // if things goes wrong we will show Toast
                    Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                }
            } else {
                play = 0;
                try {
                    mediaPlayer.setDataSource(MainActivity.this, Uri.parse("android.resource://" + getPackageName() + "/res/raw/"
                            + getResources().getResourceName(playList[play])));
                    mediaPlayer.start();
                } catch (IOException e) {
                    Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                }
            }
        }
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.play_toggle_ratarqiq1:
                // you can check to see if the song that we want to play is already being played
                // so that in that case you can pause it and resume on next click later 
                // but I leave it to you for the implementation
                playMusic(0);
                break;
            case R.id.play_toggle_ratarqiq2:
                playMusic(1);
                break;
            case R.id.play_toggle_ratarqiq3:
                playMusic(2);
                break;
        }
    }

    private void playMusic(int position) {
        // with the code below, for the first time the mediaplayer wouldn't have started playing
        // and you would have already called stop() on it.
        // mediaPlayer.stop();

        // Also, assigning mediaPlayer obj to new instance will wipe out the onCompletionListener
        // you set earlier.
        // mediaPlayer = MediaPlayer.create(this, playList[position]);

        // check to see if mediaplayer is playing to reset it if it is
        if (mediaPlayer.isPlaying() || mediaPlayer.)
            mediaPlayer.reset();
        // now that it is in idle state, set data source in it
        try {
            mediaPlayer.setDataSource(this, Uri.parse("android.resource://" + getPackageName() + "/res/raw/"
                    + getResources().getResourceName(playList[position])));
            // start playback and change btn images accordingly
            mediaPlayer.start();
            changeView(position);
            // also you forgot to update play variable to indicate the index of current song
            // that is being played
            play = position;
        } catch (IOException e) {
            Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }

    private void changeView(int position) {
        // I guess this needs a little bit of modification as
        // For eg: if btn1 is clicked, btn1 should show pause img in theory
        // same goes for all other buttons
        // try to handle it yourself
        if (position == 0) {
            btn1.setImageResource(R.drawable.ic_play_circle_outline_black_24dp);
            btn2.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
            btn3.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
        } else if (position == 1) {
            btn1.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
            btn2.setImageResource(R.drawable.ic_play_circle_outline_black_24dp);
            btn3.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
        } else if (position == 2) {
            btn1.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
            btn2.setImageResource(R.drawable.ic_pause_circle_outline_black_24dp);
            btn3.setImageResource(R.drawable.ic_play_circle_outline_black_24dp);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // releasing the mediaplayer is must so that you do not leak resources
        if (mediaPlayer != null)
            mediaPlayer.release();
    }
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement