0

As a follow up to this question App Crashing when playing sounds in succession in MediaPlayer

I copied the code from onCreate into another method (named buttonTapped). The app plays the sound correctly during onCreate, but crashes without playing any sounds when the buttonTapped method is called (by tapping a button). The error message is:

unfortunately the application has stopped.

Here is the code, followed by the stack trace

public class MainActivity extends Activity implements MediaPlayer.OnCompletionListener {

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

    public void buttonTapped(View view){
        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();

    }

    @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();
        currentTrack++;

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

  530-530/com.maxd.soundtesting E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.maxd.soundtesting, PID: 530
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:4020)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4015)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
            at com.maxd.soundtesting.MainActivity.buttonTapped(MainActivity.java:22)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4015)
            at android.view.View.performClick(View.java:4780)
            at android.view.View$PerformClick.run(View.java:19866)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Community
  • 1
  • 1
Max D
  • 189
  • 11
  • 2
    Caused by: java.lang.ArrayIndexOutOfBoundsException: length=3; index=3 at com.maxd.soundtesting.MainActivity.buttonTapped(MainActivity.java:22) – kosa Aug 18 '16 at 20:45

1 Answers1

1

In button tapped, you aren't resetting the currentTrack to 0. So after onCreate has played the 3 sounds, currentTrack = 3, and that is an invalid index as current track can only equal 0, 1, or 2.

tylerjroach
  • 2,699
  • 4
  • 19
  • 24