如何解决ArrayIndexOutOfBoundsException:length = 3;索引= 3

时间:2015-10-31 18:07:46

标签: android media-player

我对android编程很新,我正在尝试解决ArrayIndexOutOfBounds错误。当我启动媒体播放器时,所有三首歌曲都将播放,但一旦完成,应用程序将崩溃。停止按钮可以工作,但不会暂停。请帮助我尽我所能去理解。请帮助     这是播放方法

public void play() {
tracks[0] = "android.resource://" + getPackageName() + "/" + R.raw.blipotron;
tracks[1] = "android.resource://" + getPackageName() + "/" + R.raw.funk_game;
tracks[2] = "android.resource://" + getPackageName() + "/" + R.raw.poofy_reel;
mediaPlayer = MediaPlayer.create(getApplicationContext(),   
Uri.parse(tracks[currentTrack]));
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.start();
}

这是停止和暂停方法

public void stopPlayer(){
    if (container == STARTED)
        mediaPlayer.stop();
    mediaPlayer.release();
}

public void pausePlayer(){
    if (container == STARTED) {
        mediaPlayer.pause();
        container = PAUSED;
    }
}

 **ArrayIndexOutOfBoundsException: length=3; index=3 at
 **onCompletion(TestPlayerMainActivity.java:167)**
**Line 167 is mp = Mediaplayer.create(this, Uri.parse(tracks[currentTrack)**
@Override
public void onCompletion(MediaPlayer mp) {
   // mp.release();
   if (currentTrack < tracks.length ) {
       currentTrack++;
       mp = MediaPlayer.create(this, Uri.parse(tracks[currentTrack]));
       mp.setOnCompletionListener(this);
       container = PREPARED;
       mp.start();
       container = STARTED;

         }
      }
  }

0 个答案:

没有答案