Search code examples
androidmedia-player

MediaPlayer on Android only plays part of my song :(


I am trying to play a file using the MediaPlay. The first time it is played, it only plays for 1 or 2 seconds then cuts out and never returns. The second time I play the song, it goes about 3 seconds. This function is in the onCreate function:

checkin.setOnCheckedChangeListener(new OnCheckedChangeListener(){
   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
   MediaPlayer mp = MediaPlayer.create(context, R.raw.my_song);
   mp.start();
   v.vibrate(500);
   myLocation.getLocation(context, locationResult);
}

Is another thread clearing overriding the song? How do I get my whole song to play?


Solution

  • It looks like you're calling start() too quickly, before player actually loads all it needs to play the song. Use setOnPreparedListener() to start playing after the song is prepared for playback.