编写一个小型音频播放器,我希望允许用户在每次点击相应按钮时播放上一首和下一首歌曲,我正在使用Listview列出所有歌曲
private int currentSongIndex = 0;
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long id) {
strNURL = videoMovieArrayList.get(position).getUrl().toString();
strNTITLE = videoMovieArrayList.get(position).getTitle().toString();
if(mediaPlayer!=null && mediaPlayer.isPlaying())
try {
mediaPlayer.stop();
} catch (Exception e) {
}
play(arg1);
textTrack.setText(strNTITLE);
}
});
}
public void play(View view){
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(strNURL);
} catch (IllegalArgumentException e) {
.......................
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
pauseButton.setEnabled(false);
playButton.setEnabled(true);
}
});
}
public void prev(View view){
Toast.makeText(MusicTrackActivity.this, "Previous song !", Toast.LENGTH_LONG).show();
if(currentSongIndex > 0){
// play(currentSongIndex - 1);
currentSongIndex = currentSongIndex - 1;
}else{
// play last song
// playSong(videoMovieArrayList.size() - 1);
currentSongIndex = videoMovieArrayList.size() - 1;
}
}
public void next(View view){
Toast.makeText(MusicTrackActivity.this, "Next song !", Toast.LENGTH_LONG).show();
if(currentSongIndex < (videoMovieArrayList.size() - 1)){
// play(currentSongIndex + 1);
currentSongIndex = currentSongIndex + 1;
}else{
// play(0);
currentSongIndex = 0;
}
}
所以现在我的查询是,每当用户点击下一步按钮时,如何切换到下一首歌曲,如上面的代码所示,我只是在显示Toast消息
最佳答案
btnNext.setOnClickListener(new View.OnClickListener() {
int currentSongIndex= 0;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(currentSongIndex < (songsList.size() - 1)){
playSong(currentSongIndex + 1);
currentSongIndex = currentSongIndex + 1;
}else{
// play first song
plysong(songindex);
}
}
});
关于android - 音频播放器-如何使用ListView播放下一首和上一首歌曲,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26858592/