Android Audio Player

6

I created a music player and it works 100% only when it reaches the end of a song it does not go to the next list. Does anyone know of a solution to my problem? (Note: the songs played and the 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;

    }
}
}
    
asked by anonymous 12.05.2015 / 16:23

1 answer

0

Dude implement the following method that is from the OnCompletionListener interface and inside it you should call the MediaPlayer with the next song in the list.

public void onCompletion (MediaPlayer arg0) {

}

Within the onCreate you should register the onCompletion by adding the code below: mp.setOnCompletionListener (this)

    
13.05.2015 / 01:47