java代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private List<MusicBean> list = new ArrayList<>();
private MyAdapter adapter;
private MediaPlayer mediaPlayer;
private int index = 0;
private Timer timer;
private int flag = 0;
private ListView listView;
private ImageView T_image_item;
private TextView T_text_name;
private SeekBar T_seekBar;
private Button T_button_set;
private Button T_button_up;
private Button T_button_start;
private Button T_button_stop;
private Button T_button_down;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
listView = (ListView) findViewById(R.id.listView);
T_image_item = (ImageView) findViewById(R.id.T_image_item);
T_text_name = (TextView) findViewById(R.id.T_text_name);
T_seekBar = (SeekBar) findViewById(R.id.T_seekBar);
T_button_set = (Button) findViewById(R.id.T_button_set);
T_button_up = (Button) findViewById(R.id.T_button_up);
T_button_start = (Button) findViewById(R.id.T_button_start);
T_button_stop = (Button) findViewById(R.id.T_button_stop);
T_button_down = (Button) findViewById(R.id.T_button_down);
T_button_set.setOnClickListener(this);
T_button_up.setOnClickListener(this);
T_button_start.setOnClickListener(this);
T_button_stop.setOnClickListener(this);
T_button_down.setOnClickListener(this);
mediaPlayer = new MediaPlayer();
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 100);
ContentResolver contentResolver = getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = contentResolver.query(uri, null, null, null, null);
while (cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
String user = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String time = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
String data = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATA));
MusicBean musicBean = new MusicBean(name,user,time,data);
if (Integer.parseInt(time)>=1000*60){
list.add(musicBean);
}
}
adapter = new MyAdapter(list,this);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
play(i);
index = i;
}
});
T_seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if (b){
mediaPlayer.seekTo(i);
}
if (seekBar.getMax() == mediaPlayer.getCurrentPosition()){
mediaPlayer.stop();
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
switch (flag){
case 0:
play(index);
break;
case 1:
next();
break;
case 2:
int i = new Random().nextInt(list.size() - 1);
play(i);
break;
}
}
});
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
next();
return false;
}
});
}
private void play(int i) {
if (timer!=null){
timer.cancel();
}
T_text_name.setText(list.get(i).getName());
T_button_start.setVisibility(View.GONE);
T_button_stop.setVisibility(View.VISIBLE);
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(list.get(i).getData());
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
T_seekBar.setMax(mediaPlayer.getDuration());
}
});
} catch (IOException e) {
e.printStackTrace();
}
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
T_seekBar.setProgress(mediaPlayer.getCurrentPosition());
}
},0,1000);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.T_button_set:
final PopupWindow popupWindow = new PopupWindow();
View inflate = getLayoutInflater().inflate(R.layout.layout_item_button, null);
View justOne = inflate.findViewById(R.id.justOne);
View addLast = inflate.findViewById(R.id.addLast);
View randerm = inflate.findViewById(R.id.randerm);
justOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
flag = 0;
popupWindow.dismiss();
}
});
addLast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
flag = 1;
popupWindow.dismiss();
}
});
randerm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
flag = 2;
popupWindow.dismiss();
}
});
popupWindow.setContentView(inflate);
popupWindow.setWidth(300);
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setOutsideTouchable(true);
popupWindow.showAsDropDown(T_button_set,-50,-600);
break;
case R.id.T_button_up:
index--;
if (index <= 0){
index = 0;
}
play(index);
break;
case R.id.T_button_start:
T_button_start.setVisibility(View.GONE);
T_button_stop.setVisibility(View.VISIBLE);
mediaPlayer.start();
break;
case R.id.T_button_stop:
T_button_start.setVisibility(View.VISIBLE);
T_button_stop.setVisibility(View.GONE);
mediaPlayer.pause();
break;
case R.id.T_button_down:
next();
break;
}
}
private void next() {
index++;
if (index > list.size() - 1){
index = 0;
}
play(index);
}
}
主页面布局
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<ListView
android:layout_weight="1"
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
<TextView
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="#dcdcdc"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@mipmap/ic_launcher"
android:id="@+id/T_image_item"
android:layout_centerVertical="true"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="歌名"
android:gravity="center_horizontal"
android:id="@+id/T_text_name"
/>
<SeekBar
android:id="@+id/T_seekBar"
android:layout_marginLeft="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/T_image_item"
android:layout_below="@id/T_text_name"
android:layout_marginTop="5dp"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_toRightOf="@id/T_image_item"
android:layout_below="@id/T_seekBar"
android:layout_width="250dp"
android:layout_height="50dp">
<Button
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:id="@+id/T_button_set"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/playbar_btn_playlist"
android:layout_below="@id/T_seekBar"
android:layout_toRightOf="@id/T_image_item"
/>
<Button
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:id="@+id/T_button_up"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/playbar_btn_prev"
android:layout_below="@id/T_seekBar"
android:layout_toRightOf="@id/T_image_item"
/>
<Button
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:id="@+id/T_button_start"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/playbar_btn_play"
android:layout_below="@id/T_seekBar"
android:layout_toRightOf="@id/T_image_item"
/>
<Button
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:id="@+id/T_button_stop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/playbar_btn_pause"
android:layout_below="@id/T_seekBar"
android:layout_toRightOf="@id/T_image_item"
android:visibility="gone"
/>
<Button
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:id="@+id/T_button_down"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/playbar_btn_next"
android:layout_below="@id/T_seekBar"
android:layout_toRightOf="@id/T_image_item"
/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
ListView布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher"
android:id="@+id/image_item"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/name"
android:text="歌名"
android:textSize="25dp"
android:layout_marginLeft="50dp"
android:layout_toRightOf="@id/image_item"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/user"
android:text="歌手"
android:textSize="20dp"
android:layout_marginLeft="50dp"
android:layout_toRightOf="@id/image_item"
android:layout_alignBottom="@id/image_item"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/time"
android:text="时长"
android:textSize="25dp"
android:layout_marginRight="20dp"
android:layout_alignParentRight="true"
android:layout_alignBottom="@id/image_item"
/>
</RelativeLayout>
适配器
public class MyAdapter extends BaseAdapter {
private List<MusicBean> list;
private Context context;
private LayoutInflater layoutInflater;
public MyAdapter(List<MusicBean> list, Context context) {
this.list = list;
this.context = context;
layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder = null;
if (view == null){
holder = new ViewHolder();
view = layoutInflater.inflate(R.layout.layout_item_listview,null);
holder.imageView = view.findViewById(R.id.image_item);
holder.textView_name = view.findViewById(R.id.name);
holder.textView_user = view.findViewById(R.id.user);
holder.textView_time = view.findViewById(R.id.time);
view.setTag(holder);
}else {
holder = (ViewHolder) view.getTag();
}
holder.imageView.setImageResource(R.mipmap.ic_launcher);
holder.textView_name.setText(list.get(i).getName());
holder.textView_user.setText(list.get(i).getUser());
try {
int time = Integer.parseInt(list.get(i).getTime());
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss");
String format = simpleDateFormat.format(time);
holder.textView_time.setText(format);
} catch (NumberFormatException e) {
e.printStackTrace();
}
return view;
}
static class ViewHolder{
private ImageView imageView;
private TextView textView_name;
private TextView textView_user;
private TextView textView_time;
}
}