Android使用ListView实现播放列表和音乐播放器的代码和单布局代码
时间: 2024-02-15 09:04:45 浏览: 205
Android之ListView代码
好的,下面是一个简单的Android音乐播放器的代码示例,包含了列表展示和音乐播放功能:
1. 首先是布局文件,这里使用了ListView来展示音乐列表,一个Button用于播放音乐:
```xml
<!-- activity_music.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">
<ListView
android:id="@+id/music_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/play_button" />
<Button
android:id="@+id/play_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Play" />
</RelativeLayout>
```
2. 接下来是单个列表项的布局文件,包含了音乐名称和时长的展示:
```xml
<!-- music_item.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/music_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Music Name" />
<TextView
android:id="@+id/music_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00" />
</LinearLayout>
```
3. 接下来是Java代码,首先是Music类,用于保存音乐名称和时长信息:
```java
// Music.java
public class Music {
private String name;
private long duration;
public Music(String name, long duration) {
this.name = name;
this.duration = duration;
}
public String getName() {
return name;
}
public long getDuration() {
return duration;
}
}
```
4. 接下来是MusicAdapter类,用于将Music数据绑定到ListView的列表项上:
```java
// MusicAdapter.java
public class MusicAdapter extends ArrayAdapter<Music> {
public MusicAdapter(Context context, List<Music> musicList) {
super(context, 0, musicList);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.music_item, parent, false);
}
Music music = getItem(position);
TextView musicNameView = convertView.findViewById(R.id.music_name);
TextView musicDurationView = convertView.findViewById(R.id.music_duration);
musicNameView.setText(music.getName());
musicDurationView.setText(formatDuration(music.getDuration()));
return convertView;
}
private String formatDuration(long duration) {
long minutes = TimeUnit.MILLISECONDS.toMinutes(duration);
long seconds = TimeUnit.MILLISECONDS.toSeconds(duration) - TimeUnit.MINUTES.toSeconds(minutes);
return String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
}
}
```
5. 最后是MusicActivity类,用于展示音乐列表和播放音乐:
```java
// MusicActivity.java
public class MusicActivity extends AppCompatActivity {
private List<Music> musicList = new ArrayList<>();
private MediaPlayer mediaPlayer;
private ListView musicListView;
private Button playButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music);
// 初始化音乐列表
musicList.add(new Music("Music 1", 10000));
musicList.add(new Music("Music 2", 20000));
musicList.add(new Music("Music 3", 30000));
// 初始化列表适配器
MusicAdapter adapter = new MusicAdapter(this, musicList);
// 初始化ListView并设置适配器
musicListView = findViewById(R.id.music_list);
musicListView.setAdapter(adapter);
// 初始化播放按钮
playButton = findViewById(R.id.play_button);
playButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mediaPlayer == null) {
mediaPlayer = MediaPlayer.create(MusicActivity.this, R.raw.music);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
stopMusic();
}
});
}
if (mediaPlayer.isPlaying()) {
pauseMusic();
} else {
startMusic();
}
}
});
// 初始化音频焦点监听器
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.requestAudioFocus(new AudioManager.OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN:
// 恢复音乐播放
if (mediaPlayer != null) {
mediaPlayer.setVolume(1.0f, 1.0f);
mediaPlayer.start();
}
break;
case AudioManager.AUDIOFOCUS_LOSS:
// 停止音乐播放
stopMusic();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
// 暂停音乐播放
pauseMusic();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
// 降低音量
if (mediaPlayer != null) {
mediaPlayer.setVolume(0.2f, 0.2f);
}
break;
}
}
}, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
}
private void startMusic() {
if (mediaPlayer != null) {
mediaPlayer.start();
playButton.setText(R.string.pause);
}
}
private void pauseMusic() {
if (mediaPlayer != null) {
mediaPlayer.pause();
playButton.setText(R.string.play);
}
}
private void stopMusic() {
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
playButton.setText(R.string.play);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
stopMusic();
}
}
```
这个示例演示了如何使用ListView实现音乐播放器和播放列表。在实际应用中,您还需要添加一些额外的功能,如从本地文件获取音乐文件、添加删除音乐等。
阅读全文