Android仿QQ音乐播放界面代码
时间: 2023-09-30 16:02:03 浏览: 167
以下是一个简单的Android仿QQ音乐播放界面的代码示例:
1. 首先,在布局文件中添加以下代码:
```xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageViewAlbumArt"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:src="@drawable/album_art_placeholder" />
<TextView
android:id="@+id/textViewSongTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/imageViewAlbumArt"
android:layout_centerHorizontal="true"
android:text="Song Title"
android:textColor="@android:color/black"
android:textSize="20sp" />
<TextView
android:id="@+id/textViewArtistAlbum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textViewSongTitle"
android:layout_centerHorizontal="true"
android:text="Artist - Album"
android:textColor="@android:color/darker_gray"
android:textSize="16sp" />
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textViewArtistAlbum"
android:layout_marginTop="20dp"
android:max="100"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:progress="0"
android:thumb="@drawable/seekbar_thumb" />
<TextView
android:id="@+id/textViewCurrentTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/seekBar"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:text="00:00"
android:textColor="@android:color/darker_gray"
android:textSize="14sp" />
<TextView
android:id="@+id/textViewDuration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/seekBar"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:text="00:00"
android:textColor="@android:color/darker_gray"
android:textSize="14sp" />
<LinearLayout
android:id="@+id/linearLayoutPlayerControls"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textViewCurrentTime"
android:layout_marginTop="10dp"
android:gravity="center">
<ImageButton
android:id="@+id/imageButtonPrevious"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:padding="10dp"
android:src="@drawable/ic_skip_previous_black_24dp" />
<ImageButton
android:id="@+id/imageButtonPlayPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:padding="10dp"
android:src="@drawable/ic_play_arrow_black_24dp" />
<ImageButton
android:id="@+id/imageButtonNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:padding="10dp"
android:src="@drawable/ic_skip_next_black_24dp" />
</LinearLayout>
</RelativeLayout>
```
2. 在Java类中,添加以下代码:
```java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView imageViewAlbumArt;
private TextView textViewSongTitle, textViewArtistAlbum, textViewCurrentTime, textViewDuration;
private SeekBar seekBar;
private ImageButton imageButtonPrevious, imageButtonPlayPause, imageButtonNext;
private MediaPlayer mediaPlayer;
private Handler handler;
private Runnable runnable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageViewAlbumArt = findViewById(R.id.imageViewAlbumArt);
textViewSongTitle = findViewById(R.id.textViewSongTitle);
textViewArtistAlbum = findViewById(R.id.textViewArtistAlbum);
textViewCurrentTime = findViewById(R.id.textViewCurrentTime);
textViewDuration = findViewById(R.id.textViewDuration);
seekBar = findViewById(R.id.seekBar);
imageButtonPrevious = findViewById(R.id.imageButtonPrevious);
imageButtonPlayPause = findViewById(R.id.imageButtonPlayPause);
imageButtonNext = findViewById(R.id.imageButtonNext);
// 设置监听器
imageButtonPrevious.setOnClickListener(this);
imageButtonPlayPause.setOnClickListener(this);
imageButtonNext.setOnClickListener(this);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
mediaPlayer.seekTo(progress * 1000);
updateSeekBar();
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
// 初始化MediaPlayer和Handler
mediaPlayer = MediaPlayer.create(this, R.raw.song);
handler = new Handler();
// 设置初始状态
updateSeekBar();
updatePlayPauseButton();
updateSongInfo();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.imageButtonPrevious:
mediaPlayer.seekTo(0);
updateSeekBar();
break;
case R.id.imageButtonPlayPause:
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
} else {
mediaPlayer.start();
}
updatePlayPauseButton();
break;
case R.id.imageButtonNext:
mediaPlayer.seekTo(mediaPlayer.getDuration());
updateSeekBar();
break;
}
}
private void updateSeekBar() {
seekBar.setProgress(mediaPlayer.getCurrentPosition() / 1000);
textViewCurrentTime.setText(getFormattedTime(mediaPlayer.getCurrentPosition()));
textViewDuration.setText(getFormattedTime(mediaPlayer.getDuration()));
runnable = new Runnable() {
@Override
public void run() {
updateSeekBar();
}
};
handler.postDelayed(runnable, 1000);
}
private void updatePlayPauseButton() {
if (mediaPlayer.isPlaying()) {
imageButtonPlayPause.setImageResource(R.drawable.ic_pause_black_24dp);
} else {
imageButtonPlayPause.setImageResource(R.drawable.ic_play_arrow_black_24dp);
}
}
private void updateSongInfo() {
textViewSongTitle.setText("Song Title");
textViewArtistAlbum.setText("Artist - Album");
imageViewAlbumArt.setImageResource(R.drawable.album_art_placeholder);
}
private String getFormattedTime(int milliseconds) {
int seconds = (milliseconds / 1000) % 60;
int minutes = (milliseconds / (1000 * 60)) % 60;
return String.format("%02d:%02d", minutes, seconds);
}
@Override
protected void onDestroy() {
super.onDestroy();
mediaPlayer.release();
handler.removeCallbacks(runnable);
}
}
```
在这个示例中,我们使用了MediaPlayer来播放音乐,使用SeekBar来显示进度条,使用Handler来更新SeekBar的进度。我们还添加了三个按钮,分别用于播放上一首、播放/暂停和播放下一首歌曲。在界面上,我们显示了歌曲的封面、标题、艺术家、专辑、当前时间和总时间。请注意,这个示例中使用的音乐文件是存储在`res/raw`目录下的一个示例文件。在实际应用中,您需要使用您自己的音乐文件。
阅读全文