publicclassAndroidBuildingMusicPlayerActivityextendsActivity
implementsOnCompletionListener, SeekBar.OnSeekBarChangeListener {
|
/**
* Button Click event for Play list click event
* Launches list activity which displays list of songs
* */
btnPlaylist.setOnClickListener(newView.OnClickListener() {
@Override
publicvoidonClick(View arg0) {
Intent i =newIntent(getApplicationContext(), PlayListActivity.class);
startActivityForResult(i,100);
}
});
|
/**
* Receiving song index from playlist view
* and play the song
* */
@Override
protectedvoidonActivityResult(intrequestCode,
intresultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == 100){
currentSongIndex = data.getExtras().getInt("songIndex");
// play selected song
playSong(currentSongIndex);
}
}
|
/**
* Function to play a song
* @param songIndex - index of song
* */
publicvoid playSong(intsongIndex){
// Play song
try{
mp.reset();
mp.setDataSource(songsList.get(songIndex).get("songPath"));
mp.prepare();
mp.start();
// Displaying Song title
String songTitle = songsList.get(songIndex).get("songTitle");
songTitleLabel.setText(songTitle);
// Changing Button Image to pause image
btnPlay.setImageResource(R.drawable.btn_pause);
// set Progress bar values
songProgressBar.setProgress(0);
songProgressBar.setMax(100);
// Updating progress bar
updateProgressBar();
}catch(IllegalArgumentException e) {
e.printStackTrace();
}catch(IllegalStateException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
}
|
/**
* Forward button click event
* Forwards song specified seconds
* */
btnForward.setOnClickListener(newView.OnClickListener() {
@Override
publicvoidonClick(View arg0) {
// get current song position
intcurrentPosition = mp.getCurrentPosition();
// check if seekForward time is lesser than song duration
if(currentPosition + seekForwardTime <= mp.getDuration()){
// forward song
mp.seekTo(currentPosition + seekForwardTime);
}else{
// forward to end position
mp.seekTo(mp.getDuration());
}
}
});
|
/**
* Backward button click event
* Backward song to specified seconds
* */
btnBackward.setOnClickListener(newView.OnClickListener() {
@Override
publicvoidonClick(View arg0) {
// get current song position
intcurrentPosition = mp.getCurrentPosition();
// check if seekBackward time is greater than 0 sec
if(currentPosition - seekBackwardTime >= 0){
// forward song
mp.seekTo(currentPosition - seekBackwardTime);
}else{
// backward to starting position
mp.seekTo(0);
}
}
});
|
/**
* Next button click event
* Plays next song by taking currentSongIndex + 1
* */
btnNext.setOnClickListener(newView.OnClickListener() {
@Override
publicvoidonClick(View arg0) {
// check if next song is there or not
if(currentSongIndex < (songsList.size() - 1)){
playSong(currentSongIndex +1);
currentSongIndex = currentSongIndex +1;
}else{
// play first song
playSong(0);
currentSongIndex =0;
}
}
});
|
/**
* 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;
}
}
});
5.更新SeekBar的进度和时间
要更新进度条计时器,我实现了在后台运行一个后台线程,使用一个Handler
6.Repeat button click event(事件监听)在单击按钮重复isRepeat我们需要设置为true,反之亦然。我们也需要改变图像源重复按钮来集中状态
7.Shuffle button click event(事件监听)
“洗牌”按钮上,我们需要设置isShuffle为true,反之亦然。此外,我们需要改变图像源洗牌按钮集中状态。
8.继承 song onCompletion Listener接口
实现这个监听器很重要,当歌曲播放完毕后他会通知你,在这个方法中,我们需要自动播放下一首歌曲根据重复和shuffle条件。
AndroidBuildingMusicPlayerActivity.java
|