android音乐播放完整代码和布局文件
时间: 2024-02-22 12:00:30 浏览: 73
android音乐播放器代码
这是一个简单的Android音乐播放器的完整代码和布局文件,仅供参考:
布局文件(activity_main.xml):
```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">
<TextView
android:id="@+id/tv_song_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:text="Song Name"
android:textSize="24sp" />
<SeekBar
android:id="@+id/seek_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv_song_name"
android:layout_marginTop="16dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:progress="0" />
<TextView
android:id="@+id/tv_current_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/seek_bar"
android:layout_marginStart="16dp"
android:text="0:00"
android:textSize="16sp" />
<TextView
android:id="@+id/tv_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/seek_bar"
android:layout_alignParentEnd="true"
android:layout_marginEnd="16dp"
android:text="0:00"
android:textSize="16sp" />
<LinearLayout
android:id="@+id/ll_controls"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv_duration"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:orientation="horizontal">
<ImageButton
android:id="@+id/btn_play_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_play_arrow_black_24dp" />
<ImageButton
android:id="@+id/btn_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_stop_black_24dp" />
</LinearLayout>
</RelativeLayout>
```
Java 代码文件(MainActivity.java):
```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.SeekBar;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity {
private TextView tvSongName, tvCurrentTime, tvDuration;
private ImageButton btnPlayPause, btnStop;
private SeekBar seekBar;
private MediaPlayer mediaPlayer;
private Handler handler = new Handler();
private boolean isPlaying = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化UI元素
tvSongName = findViewById(R.id.tv_song_name);
tvCurrentTime = findViewById(R.id.tv_current_time);
tvDuration = findViewById(R.id.tv_duration);
btnPlayPause = findViewById(R.id.btn_play_pause);
btnStop = findViewById(R.id.btn_stop);
seekBar = findViewById(R.id.seek_bar);
// 初始化MediaPlayer
mediaPlayer = MediaPlayer.create(this, R.raw.song);
// 设置SeekBar的最大值为音乐文件的长度(单位:毫秒)
seekBar.setMax(mediaPlayer.getDuration());
// 更新SeekBar和时间显示的线程
Thread updateSeekBarThread = new Thread() {
@Override
public void run() {
while (true) {
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (mediaPlayer != null && isPlaying) {
handler.sendMessage(handler.obtainMessage());
}
}
}
};
updateSeekBarThread.start();
// 设置SeekBar的拖动事件监听器
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
mediaPlayer.seekTo(progress);
seekBar.setProgress(progress);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
// 设置按钮的点击事件监听器
btnPlayPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isPlaying) {
mediaPlayer.pause();
btnPlayPause.setImageResource(R.drawable.ic_play_arrow_black_24dp);
isPlaying = false;
} else {
mediaPlayer.start();
btnPlayPause.setImageResource(R.drawable.ic_pause_black_24dp);
isPlaying = true;
}
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer.seekTo(0);
mediaPlayer.pause();
btnPlayPause.setImageResource(R.drawable.ic_play_arrow_black_24dp);
isPlaying = false;
}
});
}
// 更新UI元素的方法
private void updateUI() {
int currentDuration = mediaPlayer.getCurrentPosition();
int totalDuration = mediaPlayer.getDuration();
tvCurrentTime.setText(String.format("%d:%02d",
TimeUnit.MILLISECONDS.toMinutes(currentDuration),
TimeUnit.MILLISECONDS.toSeconds(currentDuration) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(currentDuration))
));
tvDuration.setText(String.format("%d:%02d",
TimeUnit.MILLISECONDS.toMinutes(totalDuration),
TimeUnit.MILLISECONDS.toSeconds(totalDuration) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(totalDuration))
));
seekBar.setProgress(currentDuration);
}
// 处理更新SeekBar和时间显示的消息的方法
private void handleMessage(Message msg) {
updateUI();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
}
}
```
请注意,这段代码是一个简单的示例,可能不适用于所有情况。在实际应用中,您可能需要添加更多的功能和错误处理来提高应用的稳定性和用户体验。
阅读全文