8
I created a music player it is working 100% only that when it comes to the end of a song it does not pass to the next one of the list. Does anyone know a solution to my problem ? (Note: the songs played and sdcard).
public class Player extends Activity implements View.OnClickListener{
static MediaPlayer mp;
ArrayList<File> mySongs;
int position;
Uri u;
Thread updateSeekBar;
ImageButton btnVoltar, btnBackward, btnPrevious, btnNext, btnForward;
private ImageButton btnPlayPause;
private boolean isbtnPlayer = false;
SeekBar progressMusic;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_player);
btnVoltar = (ImageButton) findViewById(R.id.btnVoltar);
btnBackward = (ImageButton) findViewById(R.id.btnBackward);
btnPrevious = (ImageButton) findViewById(R.id.btnPrevious);
btnPlayPause = (ImageButton) findViewById(R.id.btnPlayPause);
btnNext = (ImageButton) findViewById(R.id.btnNext);
btnForward = (ImageButton) findViewById(R.id.btnForward);
btnBackward.setOnClickListener(this);
btnPrevious.setOnClickListener(this);
btnPlayPause.setOnClickListener(this);
btnNext.setOnClickListener(this);
btnForward.setOnClickListener(this);
progressMusic = (SeekBar) findViewById(R.id.progressMusic);
updateSeekBar = new Thread(){
@Override
public void run() {
int totalDuration = mp.getDuration();
int currentPosition = 0;
while (currentPosition < totalDuration){
try {
sleep(500);
currentPosition = mp.getCurrentPosition();
progressMusic.setProgress(currentPosition);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
if(mp!=null){
mp.stop();
mp.release();
}
Intent i = getIntent();
Bundle b = i.getExtras();
mySongs = (ArrayList) b.getParcelableArrayList("songlist");
position = b.getInt("pos", 0);
u = Uri.parse(mySongs.get(position).toString());
mp = MediaPlayer.create(getApplicationContext(), u);
mp.start();
progressMusic.setMax(mp.getDuration());
updateSeekBar.start();
progressMusic.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
mp.seekTo(seekBar.getProgress());
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
});
btnVoltar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent home = new Intent(Player.this, Home.class);
Player.this.startActivity(home);
Player.this.finish();
}
});
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id){
case R.id.btnPlayPause:
if(mp.isPlaying()){
isbtnPlayer = true;
btnPlayPause.setImageResource(R.drawable.player);
mp.pause();
}else{
isbtnPlayer = true;
btnPlayPause.setImageResource(R.drawable.pause);
mp.start();
}break;
case R.id.btnForward:
mp.seekTo(mp.getCurrentPosition()+5000);
break;
case R.id.btnBackward:
mp.seekTo(mp.getCurrentPosition()-5000);
break;
case R.id.btnNext:
mp.stop();
mp.release();
position = (position+1) %mySongs.size();
u = Uri.parse(mySongs.get(position).toString());
mp = MediaPlayer.create(getApplicationContext(), u);
mp.start();
progressMusic.setMax(mp.getDuration());
break;
case R.id.btnPrevious:
mp.stop();
mp.release();
position = (position-1 < 0)? mySongs.size()-1: position-1;
u = Uri.parse(mySongs.get(position).toString());
mp = MediaPlayer.create(getApplicationContext(), u);
mp.start();
progressMusic.setMax(mp.getDuration());
break;
}
}
}
At the time I added the
mp.setOnCompletionListener(this);
on onCreate it presented errorThe method setOnCompletionListener(MediaPlayer.OnCompletionListener) in the type MediaPlayer is not applicable for the arguments (Player)
, within thepublic void onCompletion(MediaPlayer arg0) {

}
I passed the following methodmp.stop();
 mp.release();
 position = (position+1) %mySongs.size();
 u = Uri.parse(mySongs.get(position).toString());
 mp = MediaPlayer.create(getApplicationContext(), u);
 mp.start();
 progressMusic.setMax(mp.getDuration());
to call the next song, this right ?– Itallo Freire