android实现音乐播放器的代码和布局示例
时间: 2024-02-15 18:03:12 浏览: 69
android音乐播放器代码
以下是一个简单的Android音乐播放器的代码和布局示例:
布局文件:
```xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/player_controls"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:padding="16dp">
<ImageButton
android:id="@+id/btn_previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_skip_previous" />
<ImageButton
android:id="@+id/btn_play_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:src="@drawable/ic_play" />
<ImageButton
android:id="@+id/btn_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_skip_next" />
<SeekBar
android:id="@+id/seek_bar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:max="100"
android:paddingLeft="16dp"
android:paddingRight="16dp" />
<TextView
android:id="@+id/txt_current_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00"
android:textSize="12sp" />
</LinearLayout>
<ImageView
android:id="@+id/cover_art"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/player_controls"
android:scaleType="centerCrop"
android:src="@drawable/default_cover_art" />
</RelativeLayout>
```
代码文件:
```java
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity {
private static final int UPDATE_PROGRESS = 0;
private static final int SEEK_FORWARD_TIME = 10000;
private static final int SEEK_BACKWARD_TIME = 10000;
private MediaPlayer mediaPlayer;
private Handler handler;
private boolean isPlaying = false;
private ImageButton btnPlayPause;
private ImageView coverArt;
private TextView txtCurrentTime;
private SeekBar seekBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPlayPause = findViewById(R.id.btn_play_pause);
coverArt = findViewById(R.id.cover_art);
txtCurrentTime = findViewById(R.id.txt_current_time);
seekBar = findViewById(R.id.seek_bar);
mediaPlayer = MediaPlayer.create(this, R.raw.sample_song);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
stopPlayback();
}
});
handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message message) {
if (message.what == UPDATE_PROGRESS) {
updateProgress();
}
return false;
}
});
btnPlayPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isPlaying) {
pausePlayback();
} else {
startPlayback();
}
}
});
ImageButton btnNext = findViewById(R.id.btn_next);
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
nextTrack();
}
});
ImageButton btnPrevious = findViewById(R.id.btn_previous);
btnPrevious.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
previousTrack();
}
});
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if (b) {
mediaPlayer.seekTo(i * 1000);
txtCurrentTime.setText(formatTime(mediaPlayer.getCurrentPosition()));
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
handler.removeMessages(UPDATE_PROGRESS);
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
handler.sendEmptyMessage(UPDATE_PROGRESS);
}
});
updateProgress();
}
private void startPlayback() {
mediaPlayer.start();
isPlaying = true;
btnPlayPause.setImageResource(R.drawable.ic_pause);
updateProgress();
}
private void pausePlayback() {
mediaPlayer.pause();
isPlaying = false;
btnPlayPause.setImageResource(R.drawable.ic_play);
handler.removeMessages(UPDATE_PROGRESS);
}
private void stopPlayback() {
mediaPlayer.stop();
mediaPlayer.prepareAsync();
isPlaying = false;
btnPlayPause.setImageResource(R.drawable.ic_play);
seekBar.setProgress(0);
txtCurrentTime.setText(formatTime(0));
}
private void nextTrack() {
// TODO: implement
}
private void previousTrack() {
// TODO: implement
}
private void updateProgress() {
int currentPosition = mediaPlayer.getCurrentPosition();
int duration = mediaPlayer.getDuration();
seekBar.setProgress(currentPosition / 1000);
txtCurrentTime.setText(formatTime(currentPosition));
if (currentPosition < duration) {
handler.sendEmptyMessageDelayed(UPDATE_PROGRESS, 1000);
}
}
private String formatTime(int milliseconds) {
return String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(milliseconds),
TimeUnit.MILLISECONDS.toSeconds(milliseconds) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(milliseconds))
);
}
@Override
protected void onDestroy() {
super.onDestroy();
mediaPlayer.release();
handler.removeCallbacksAndMessages(null);
}
}
```
注意:这只是一个简单的示例,实际的音乐播放器应该实现更多的功能,如播放列表、随机播放、循环播放等。
阅读全文