2

I found the following code as an answer to a question here Playing multiple files in MediaPlayer one after other In android

When I run it, the 3 sounds are played but then the app closes with the message

unfortunately the application has stopped.

Is there something missing inside the onCompletion method?

public class MainActivity extends Activity implements MediaPlayer.OnCompletionListener {

    int[] tracks = new int[3];
    int currentTrack = 0;
    private MediaPlayer mediaPlayer = null;

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

        tracks[0] = R.raw.p46;
        tracks[1] = R.raw.p52;
        tracks[2] = R.raw.p55;
        mediaPlayer = MediaPlayer.create(getApplicationContext(), tracks[currentTrack]);
        mediaPlayer.setOnCompletionListener(this);
        mediaPlayer.start();
    }

    public void onCompletion(MediaPlayer arg0) {
        arg0.release();
        if (currentTrack < tracks.length) {
            currentTrack++;
            arg0 = MediaPlayer.create(getApplicationContext(), tracks[currentTrack]);
            arg0.setOnCompletionListener(this);
            arg0.start();
        }
    }
Community
  • 1
  • 1
Max D
  • 189
  • 11
  • 2
    I think this just an error with your if statement. You're incrementing `currentTrack` after range checking, resulting in an out of bounds exception. – Jack Aug 18 '16 at 18:42
  • I think the error mught be coming from ` arg0.setOnCompletionListener(this);` in your onCompletion method try removing it and see if it doesnt throw an error after the first song – Amr El Aswar Aug 18 '16 at 18:44

1 Answers1

3

Change this:

public void onCompletion(MediaPlayer arg0) {
        arg0.release();
        if (currentTrack < tracks.length) {
            currentTrack++;
            arg0 = MediaPlayer.create(getApplicationContext(), tracks[currentTrack]);
            arg0.setOnCompletionListener(this);
            arg0.start();
        }
    }

to :

public void onCompletion(MediaPlayer arg0) {
        arg0.release();
        currentTrack++;
        if (currentTrack < tracks.length) {

            arg0 = MediaPlayer.create(getApplicationContext(), tracks[currentTrack]);
            arg0.setOnCompletionListener(this);
            arg0.start();
        }
    }

basically you are getting a ArrayIndexOutofBoundException becoz at last currentTrack is 3 and and there is no item at index 3 i.e 4th item

JAAD
  • 12,349
  • 7
  • 36
  • 57
  • This fixed the problem, but now if I move the sound playing code from onCreate to buttonTapped, the app crashs again. ' public void buttonTapped(View view){ tracks[0] = R.raw.pianoff46; tracks[1] = R.raw.pianoff52; tracks[2] = R.raw.pianoff55; mediaPlayer = MediaPlayer.create(getApplicationContext(), tracks[currentTrack]); mediaPlayer.setOnCompletionListener(this); mediaPlayer.start(); } – Max D Aug 18 '16 at 19:07
  • 1
    that is a different issue pls open another question for that and also pls post stacktrace this time – JAAD Aug 18 '16 at 19:10
  • 1
    there is a check button left to each answer click that – JAAD Aug 18 '16 at 19:15