Android构建音频播放器教程(三)
/** * Back button click event * Plays previous song by currentSongIndex - 1 * */ btnPrevious.setOnClickListener(newView.OnClickListener() { @Override publicvoidonClick(View arg0) { if(currentSongIndex > 0){ playSong(currentSongIndex - 1); currentSongIndex = currentSongIndex - 1; }else{ // play last song playSong(songsList.size() - 1); currentSongIndex = songsList.size() - 1; } } });/** * Update timer on seekbar * */ publicvoidupdateProgressBar() { mHandler.postDelayed(mUpdateTimeTask,100); } /** * Background Runnable thread * */ privateRunnable mUpdateTimeTask = new Runnable() { publicvoidrun() { longtotalDuration = mp.getDuration(); longcurrentDuration = mp.getCurrentPosition(); // Displaying Total Duration time songTotalDurationLabel.setText(""+utils.milliSecondsToTimer(totalDuration)); // Displaying time completed playing songCurrentDurationLabel.setText(""+utils.milliSecondsToTimer(currentDuration)); // Updating progress bar intprogress = (int)(utils.getProgressPercentage(currentDuration, totalDuration)); //Log.d("Progress", ""+progress); songProgressBar.setProgress(progress); // Running this thread after 100 milliseconds mHandler.postDelayed(this,100); } }; /** * * */ @Override publicvoidonProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) { } /** * When user starts moving the progress handler * */ @Override publicvoidonStartTrackingTouch(SeekBar seekBar) { // remove message Handler from updating progress bar mHandler.removeCallbacks(mUpdateTimeTask); } /** * When user stops moving the progress hanlder * */ @Override publicvoidonStopTrackingTouch(SeekBar seekBar) { mHandler.removeCallbacks(mUpdateTimeTask); inttotalDuration = mp.getDuration(); intcurrentPosition = utils.progressToTimer(seekBar.getProgress(), totalDuration); // forward or backward to certain seconds mp.seekTo(currentPosition); // update timer progress again updateProgressBar(); }/** * Button Click event for Repeat button * Enables repeat flag to true * */ btnRepeat.setOnClickListener(newView.OnClickListener() { @Override publicvoidonClick(View arg0) { if(isRepeat){ isRepeat = false; Toast.makeText(getApplicationContext(),"Repeat is OFF", Toast.LENGTH_SHORT).show(); btnRepeat.setImageResource(R.drawable.btn_repeat); }else{ // make repeat to true isRepeat = true; Toast.makeText(getApplicationContext(),"Repeat is ON", Toast.LENGTH_SHORT).show(); // make shuffle to false isShuffle = false; btnRepeat.setImageResource(R.drawable.btn_repeat_focused); btnShuffle.setImageResource(R.drawable.btn_shuffle); } } });/** * Button Click event for Shuffle button * Enables shuffle flag to true * */ btnShuffle.setOnClickListener(newView.OnClickListener() { @Override publicvoidonClick(View arg0) { if(isShuffle){ isShuffle = false; Toast.makeText(getApplicationContext(),"Shuffle is OFF", Toast.LENGTH_SHORT).show(); btnShuffle.setImageResource(R.drawable.btn_shuffle); }else{ // make repeat to true isShuffle=true; Toast.makeText(getApplicationContext(),"Shuffle is ON", Toast.LENGTH_SHORT).show(); // make shuffle to false isRepeat = false; btnShuffle.setImageResource(R.drawable.btn_shuffle_focused); btnRepeat.setImageResource(R.drawable.btn_repeat); } } });/** * On Song Playing completed * if repeat is ON play same song again * if shuffle is ON play random song * */ @Override publicvoidonCompletion(MediaPlayer arg0) { // check for repeat is ON or OFF if(isRepeat){ // repeat is on play same song again playSong(currentSongIndex); }elseif(isShuffle){ // shuffle is on - play a random song Random rand = new Random(); currentSongIndex = rand.nextInt((songsList.size() - 1) - 0 + 1) + 0; playSong(currentSongIndex); }else{ // no repeat or shuffle ON - play next song if(currentSongIndex < (songsList.size() - 1)){ playSong(currentSongIndex + 1); currentSongIndex = currentSongIndex + 1; }else{ // play first song playSong(0); currentSongIndex = 0; } } }