Android构建音频播放器教程(三)
10. 为Audio Player写相关类文件
打开你的主要活动类(MainActivity)处理主要player界面,并且继承 OnCompletionListener, SeekBar.OnSeekBarChangeListener.在这种情况下,我的主要活动名称是AndroidBuildingMusicPlayerActivity。
AndroidBuildingMusicPlayerActivity.java publicclassAndroidBuildingMusicPlayerActivityextendsActivity
implementsOnCompletionListener, SeekBar.OnSeekBarChangeListener {
现在声明所有变量需要为此音频播放器类。(audio player class)
AndroidBuildingMusicPlayerActivity.java
publicclassAndroidBuildingMusicPlayerActivityextendsActivity
implementsOnCompletionListener, SeekBar.OnSeekBarChangeListener {
privateImageButton btnPlay;
privateImageButton btnForward;
privateImageButton btnBackward;
privateImageButton btnNext;
privateImageButton btnPrevious;
privateImageButton btnPlaylist;
privateImageButton btnRepeat;
privateImageButton btnShuffle;
privateSeekBar songProgressBar;
privateTextView songTitleLabel;
privateTextView songCurrentDurationLabel;
privateTextView songTotalDurationLabel;
// Media Player
private MediaPlayer mp;
// Handler to update UI timer, progress bar etc,.
privateHandler mHandler =newHandler();;
privateSongsManager songManager;
privateUtilities utils;
privateintseekForwardTime =5000;// 5000 milliseconds
privateintseekBackwardTime =5000;// 5000 milliseconds
privateintcurrentSongIndex =0;
privatebooleanisShuffle =false;
privatebooleanisRepeat =false;
privateArrayList<HashMap<String, String>> songsList =newArrayList<HashMap<String, String>>();
现在引用XML布局的所有按钮,图像类
AndroidBuildingMusicPlayerActivity.java
// All player buttons
btnPlay = (ImageButton) findViewById(R.id.btnPlay);
btnForward = (ImageButton) findViewById(R.id.btnForward);
btnBackward = (ImageButton) findViewById(R.id.btnBackward);
btnNext = (ImageButton) findViewById(R.id.btnNext);
btnPrevious = (ImageButton) findViewById(R.id.btnPrevious);
btnPlaylist = (ImageButton) findViewById(R.id.btnPlaylist);
btnRepeat = (ImageButton) findViewById(R.id.btnRepeat);
btnShuffle = (ImageButton) findViewById(R.id.btnShuffle);
songProgressBar = (SeekBar) findViewById(R.id.songProgressBar);
songTitleLabel = (TextView) findViewById(R.id.songTitle);
songCurrentDurationLabel = (TextView) findViewById(R.id.songCurrentDurationLabel);
songTotalDurationLabel = (TextView) findViewById(R.id.songTotalDurationLabel);
// Mediaplayer
mp =newMediaPlayer();
songManager =newSongsManager();
utils =newUtilities();
// Listeners
songProgressBar.setOnSeekBarChangeListener(this);// Important
mp.setOnCompletionListener(this);// Important
// Getting all songs list
songsList = songManager.getPlayList();
1.加载播放列表(PlayList)
为playlist button写单击事件侦听器,在点击播放按钮时我们需要加载PlayListAcitivity.java ,从列表中选择一个特定的歌曲上我们需要得到songIndex。
AndroidBuildingMusicPlayerActivity.java /**
* 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);
}
});
接收选定的歌曲索引增加以下功能(请确保您添加此功能以外的onCreate方法)
AndroidBuildingMusicPlayerActivity.java /**
* 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);
}
}
2.Playing Song(播放歌曲)
添加以下功能添加到你的类,这个函数接受songIndex作为参数并播放。当开始播放一首歌切换播放按钮暂停按钮状态。
AndroidBuildingMusicPlayerActivity.java /**
* 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
&nbs
补充:移动开发 , Android ,