Android使用ListView实现播放列表和音乐播放器的代码和布局代码,使用单布局
时间: 2024-02-15 20:04:54 浏览: 120
Android之ListView代码
以下是一个简单的使用 `ListView` 实现播放列表和音乐播放器的代码和布局代码,使用单一布局:
布局代码 `activity_main.xml`:
```xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 播放器控制区域 -->
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- 播放/暂停按钮 -->
<Button
android:id="@+id/btn_play_pause"
android:text="Play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<!-- 歌曲名称和时间 -->
<TextView
android:id="@+id/tv_song_name"
android:text="Song Name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:id="@+id/tv_song_time"
android:text="00:00 / 00:00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<!-- 播放列表 -->
<ListView
android:id="@+id/listview_playlist"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:dividerHeight="1dp"/>
</LinearLayout>
```
Java 代码 `MainActivity.java`:
```java
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity implements AdapterView.OnItemClickListener, MediaPlayer.OnCompletionListener {
private List<String> songList = new ArrayList<String>();
private ListView listView;
private ArrayAdapter<String> adapter;
private MediaPlayer mediaPlayer = new MediaPlayer();
private Button playPauseButton;
private TextView songNameTextView;
private TextView songTimeTextView;
private int currentSongIndex = -1;
private boolean isPlaying = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化播放列表
songList.add("Song 1");
songList.add("Song 2");
songList.add("Song 3");
// 初始化ListView
listView = (ListView) findViewById(R.id.listview_playlist);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, songList);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
// 初始化播放器控制区域
playPauseButton = (Button) findViewById(R.id.btn_play_pause);
songNameTextView = (TextView) findViewById(R.id.tv_song_name);
songTimeTextView = (TextView) findViewById(R.id.tv_song_time);
playPauseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isPlaying) {
pause();
} else {
play();
}
}
});
mediaPlayer.setOnCompletionListener(this);
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
currentSongIndex = i;
playSong(currentSongIndex);
updateUI();
}
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
currentSongIndex++;
if (currentSongIndex >= songList.size()) {
currentSongIndex = 0;
}
playSong(currentSongIndex);
updateUI();
}
private void play() {
if (mediaPlayer.isPlaying()) {
return;
}
if (currentSongIndex == -1) {
currentSongIndex = 0;
playSong(currentSongIndex);
} else {
mediaPlayer.start();
}
isPlaying = true;
playPauseButton.setText("Pause");
}
private void pause() {
if (!mediaPlayer.isPlaying()) {
return;
}
mediaPlayer.pause();
isPlaying = false;
playPauseButton.setText("Play");
}
private void playSong(int index) {
mediaPlayer.reset();
try {
mediaPlayer.setDataSource("/path/to/" + songList.get(index) + ".mp3");
mediaPlayer.prepare();
mediaPlayer.start();
isPlaying = true;
} catch (IOException e) {
e.printStackTrace();
}
}
private void updateUI() {
songNameTextView.setText(songList.get(currentSongIndex));
int duration = mediaPlayer.getDuration();
int currentPosition = mediaPlayer.getCurrentPosition();
int minutes = currentPosition / 1000 / 60;
int seconds = (currentPosition / 1000) % 60;
String currentPositionString = String.format("%d:%02d", minutes, seconds);
minutes = duration / 1000 / 60;
seconds = (duration / 1000) % 60;
String durationString = String.format("%d:%02d", minutes, seconds);
songTimeTextView.setText(currentPositionString + " / " + durationString);
}
}
```
需要注意的是,需要在 AndroidManifest.xml 文件中添加读取存储权限,以便访问音乐文件。同时,获取音乐文件的播放时长需要在 `MediaPlayer` 对象准备好后才能获取,可以使用 `setOnPreparedListener` 方法来监听。
阅读全文