1.编写主页面,使用listview组件放置音乐列表信息
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:background="#D8D8D8">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/local" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.example.myapplication.MyListVIew
android:id="@+id/video_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:id="@+id/local"
android:background="#ECC8C8"
android:layout_alignParentBottom="true">
<ImageView
android:id="@+id/img"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/bold"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"/>
<TextView
android:id="@+id/song"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="告白气球"
android:layout_toRightOf="@id/img"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:textColor="#000"
android:textSize="15sp"/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="周杰伦"
android:layout_toRightOf="@id/img"
android:layout_marginTop="40dp"
android:layout_marginLeft="20dp"
android:textColor="#000"
android:textSize="12sp"
/>
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3:30"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textColor="#000"
android:textSize="12sp"
/>
<TextView
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一曲"
android:textSize="12sp"
android:textColor="#000"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"/>
<TextView
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停"
android:textSize="12sp"
android:textColor="#000"
android:layout_toLeftOf="@id/next"
android:layout_centerVertical="true"
android:enabled="false"
android:layout_marginRight="20dp"/>
<TextView
android:id="@+id/up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上一曲"
android:textSize="12sp"
android:textColor="#000"
android:layout_toLeftOf="@id/stop"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"/>
</RelativeLayout>
</RelativeLayout>
2.写Bean类、Adapter适配器
package com.example.myapplication;
import java.io.Serializable;
public class VideoBean implements Serializable {
private int id;
private String song;
private String singer;
private String album;
private String time;
private int path;
public VideoBean(int id, String song, String singer, String album, String time, int path) {
this.id = id;
this.song = song;
this.singer = singer;
this.album = album;
this.time = time;
this.path = path;
}
public String getTime() {
return time;
}
public String getAlbum() {
return album;
}
public int getId() {
return id;
}
public int getPath() {
return path;
}
public String getSinger() {
return singer;
}
public String getSong() {
return song;
}
}
package com.example.myapplication;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.List;
public class VideoAdapter extends BaseAdapter {
private List<VideoBean> list;
private Context context;
public VideoAdapter(List<VideoBean> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView==null){
convertView= LayoutInflater.from(context).inflate(R.layout.adaper_video,parent,false);
}
VideoBean bean=list.get(position);
TextView id=convertView.findViewById(R.id.id);
TextView song=convertView.findViewById(R.id.song);
TextView singer=convertView.findViewById(R.id.singer);
TextView album=convertView.findViewById(R.id.album);
TextView time=convertView.findViewById(R.id.time);
id.setText(bean.getId()+"");
song.setText(bean.getSong());
singer.setText(bean.getSinger());
album.setText(bean.getAlbum());
time.setText(bean.getTime());
return convertView;
}
}
3.写listview的子布局页面
<?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="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@drawable/shape10"
android:orientation="horizontal"
android:paddingLeft="30dp"
android:paddingRight="30dp">
<TextView
android:id="@+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textSize="35sp"
android:textColor="#000"
android:layout_centerVertical="true"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerVertical="true"
android:layout_marginLeft="50dp"
>
<TextView
android:id="@+id/song"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="告白气球"
android:textSize="25sp"
android:textColor="#000"
android:layout_gravity="center"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<TextView
android:id="@+id/singer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="许嵩"
android:textSize="20sp"
android:textColor="#000"
android:layout_gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="|"
android:textSize="20sp"
android:textColor="#000"
android:layout_marginLeft="10dp"
android:layout_gravity="center"/>
<TextView
android:id="@+id/album"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="专辑名称"
android:textSize="20sp"
android:textColor="#000"
android:layout_marginLeft="10dp"
android:layout_gravity="center"/>
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_alignParentRight="true"
android:text="4:20"
android:textColor="#000"
android:layout_alignParentBottom="true"
android:layout_marginBottom="15dp"/>
</RelativeLayout>
</LinearLayout>
下载几个mp3音乐导入raw中
4.编写主页面java文件
package com.example.myapplication;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.media.MediaParser;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private TextView song,name,next,stop,up,time;
private ListView video_list;
private List<VideoBean> data;
private MediaPlayer mediaPlayer;
private int num=0;
private int id=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
data=new ArrayList<>();
data.add(new VideoBean(1,"七月上","Jam","七月上","3:10",R.raw.a1));
data.add(new VideoBean(2,"大摇大摆迎春来","大张伟","七月上","2:54",R.raw.a2));
data.add(new VideoBean(3,"抱歉","科德夏萍","陈慧琳","3:56",R.raw.a3));
data.add(new VideoBean(4,"白色恋人","游鸿铭","游鸿铭","4:48",R.raw.a4));
data.add(new VideoBean(4,"白色恋人","游鸿铭","游鸿铭","4:48",R.raw.a4));
data.add(new VideoBean(4,"白色恋人","游鸿铭","游鸿铭","4:48",R.raw.a4));
video_list.setAdapter(new VideoAdapter(data,MainActivity.this));
video_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
VideoBean bean=data.get(position);
song.setText(bean.getSong());
name.setText(bean.getSinger());
time.setText(bean.getTime());
if (mediaPlayer != null) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.release();
mediaPlayer = null;
}
mediaPlayer=MediaPlayer.create(MainActivity.this,bean.getPath());
mediaPlayer.start();
stop.setEnabled(true);
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
num++;
if (num%2==1){
mediaPlayer.pause();
stop.setText("开始");
}else if (num%2==0){
stop.setText("暂停");
mediaPlayer.start();
}
}
});
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mediaPlayer.reset();
id = (id % data.size()) + 1;
if (id >= data.size()) {
id = 0;
}
VideoBean bean = data.get(id);
song.setText(bean.getSong());
name.setText(bean.getSinger());
time.setText(bean.getTime());
mediaPlayer = MediaPlayer.create(MainActivity.this, bean.getPath());
mediaPlayer.start();
// 恢复"暂停"按钮的文本,因为我们现在正在播放新的歌曲
stop.setText("暂停");
stop.setEnabled(true);
}
});
up.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mediaPlayer.reset();
id = id-1;
// 如果索引超出了列表大小,将其重置为0(列表的第一首歌曲)
if (id <0) {
id = data.size() - 1;
}
VideoBean bean = data.get(id);
song.setText(bean.getSong());
name.setText(bean.getSinger());
time.setText(bean.getTime());
mediaPlayer = MediaPlayer.create(MainActivity.this, bean.getPath());
mediaPlayer.start();
// 恢复"暂停"按钮的文本,因为我们现在正在播放新的歌曲
stop.setText("暂停");
stop.setEnabled(true);
}
});
}
private void init(){
song=findViewById(R.id.song);
name=findViewById(R.id.name);
next=findViewById(R.id.next);
stop=findViewById(R.id.stop);
up=findViewById(R.id.up);
time=findViewById(R.id.time);
video_list=findViewById(R.id.video_list);
}
}