1600802010-音乐播放器
视频:
关键代码
播放,暂停,停止,下一首,上一首代码:
private class ButtonClick implements View.OnClickListener {
@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.btnItems:
Intent intent = new Intent(Intent.ACTION_PICK,
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQUEST_CODE_AUDIO_PICK);
break;
case R.id.btnPlay:
if (player == null) {
Toast.makeText(getApplicationContext(), "请先选择音乐!",
Toast.LENGTH_SHORT).show();
return;
}
if (player != null && player.isPlaying()) {
player.pause();
btnPlay.setImageResource(R.drawable.btn_play_image);
} else {
new Thread(new ProgressRun()).start();
player.start();
btnPlay.setImageResource(R.drawable.btn_pause_image);
}
break;
case R.id.btnStop:
if (player != null && player.isPlaying()) {
player.stop();
player.release();
player = null;
btnPlay.setImageResource(R.drawable.btn_play_image);
tvMusicTitle.setText("暂无曲目");
}
break;
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_AUDIO_PICK) {
if (resultCode == RESULT_OK) {
uri = data.getData();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(uri, null, null, null, null);
if (cur.moveToNext()) {
tvMusicTitle
.setText("当前曲目:"
+ cur.getString(cur
.getColumnIndexOrThrow((MediaStore.Audio.Media.TITLE))));
sbProgress.setMax(cur.getInt(cur.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)));
sbProgress.setProgress(0);
System.out.println(">>> " + sbProgress.getProgress());
player = MediaPlayer.create(getApplicationContext(), uri);
player.setOnCompletionListener(new MusicCompletionListener());
}
} else {
Toast.makeText(getApplicationContext(), "没有获取到音乐!",
Toast.LENGTH_SHORT).show();
}
}