我想制作一个音频播放器,在android studio中,我已经完成了所有步骤,例如播放,暂停,前进歌曲,倒退歌曲,但是现在我的问题是设置两个按钮,一个用于下一首歌,另一个用于内存列表中的上一首歌曲..如何设置?请任何人帮助我。
我的代码:
public class MainActivity extends AppCompatActivity {
ImageButton playBtn;
SeekBar positionBar;
TextView elapsedTimelabel;
TextView remainingTimeLabel;
MediaPlayer mp;
int totalTime, next_song;
String position_of_song;
String elapsedTime;
private boolean isMovingSeekBar = false;
ArrayList<String> arraylist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arraylist=new ArrayList<>();
getMusic();
position_of_song = getIntent().getStringExtra("songID");
playBtn = findViewById(R.id.play_pause_button);
elapsedTimelabel = findViewById(R.id.totalTime);
remainingTimeLabel = findViewById(R.id.remaining_time);
positionBar = findViewById(R.id.seek_bar);
//MediaPlayer
mp = MediaPlayer.create(this,Uri.parse(position_of_song));
mp.setLooping(true);
mp.seekTo(0);
mp.setVolume(0.5f,0.5f);
totalTime = mp.getDuration();
//Seek Bar
positionBar = findViewById(R.id.seek_bar);
positionBar.setMax(totalTime);
positionBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (isMovingSeekBar) {
}
mp.seekTo(progress);
// positionBar.setProgress(progress);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
//thread update position Bar
new Thread(new Runnable() {
@Override
public void run() {
while (mp != null) {
try {
Message msg = new Message();
msg.what = mp.getCurrentPosition();
handler.sendMessage(msg);
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
}).start();
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
int currentPosition = msg.what;
//update position bar
positionBar.setProgress(currentPosition);
super.handleMessage(msg);
//update labels
elapsedTime = createTimeLabel(currentPosition);
elapsedTimelabel.setText(elapsedTime);
String remainingTime = createTimeLabel(totalTime - currentPosition);
remainingTimeLabel.setText(remainingTime);
}
};
//getMusic from memory
public void getMusic(){
ContentResolver contentResolver=getContentResolver();
Uri uri= MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cursor=contentResolver.query(uri,null,null,null,null);
if(cursor!=null && cursor.moveToFirst()){
int songTitle=cursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
int songArtist=cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
int songData=cursor.getColumnIndex(MediaStore.Audio.Media.DATA);
do{
String currentTitle=cursor.getString(songTitle);
String currentArtist=cursor.getString(songArtist);
String currentData=cursor.getString(songData);
arraylist.add(currentTitle + "\n" + currentArtist + "\n" + currentData);
Toast.makeText(this,currentData, Toast.LENGTH_SHORT).show();
}while (cursor.moveToNext());
}
}
public String createTimeLabel(int time) {
String timeLabel = "";
int min = time / 1000 / 60;
int sec = time / 1000 % 60;
timeLabel = min + ":";
if (sec < 10) timeLabel += "0";
timeLabel += sec;
return timeLabel;
}
private void pausePlaying() {
positionBar.setMax(mp.getDuration());
if(mp.isPlaying()){
playBtn.setBackgroundResource(R.drawable.play);
mp.pause();
}
else {
playBtn.setBackgroundResource(R.drawable.pause_icon);
mp.start();
}
}
public void setPlayBtn(View view) {
//playing
if (!mp.isPlaying()) {
mp.start();
playBtn.setBackgroundResource(R.drawable.pause_icon);
//pause
} else {
pausePlaying();
}
}
public void reverseSong(View view) {
positionBar.setMax(mp.getDuration());
mp.seekTo(mp.getCurrentPosition() - 5000);
}
public void PreviousSong(View view) {
}
public void NextSong(View view) {
}
public void FarwardSong(View view) {
positionBar.setMax(mp.getDuration());
mp.seekTo(mp.getCurrentPosition() + 5000);
}
}