第一步activity—main.xml
<?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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:id="@+id/local_music_bottomlayout">
<ImageView
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#9933FA"/>
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@mipmap/icon_song"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:id="@+id/local_music_bottom_iv_icon"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/local_music_bottom_tv_song"
android:text=""
android:layout_toRightOf="@+id/local_music_bottom_iv_icon"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:textSize="16sp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/local_music_bottom_tv_singer"
android:text=""
android:textSize="12sp"
android:layout_below="@+id/local_music_bottom_tv_song"
android:layout_alignLeft="@+id/local_music_bottom_tv_song"
android:layout_marginTop="10dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/local_music_bottom_iv_next"
android:layout_centerVertical="true"
android:src="@mipmap/icon_next"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/local_music_bottom_iv_play"
android:layout_centerVertical="true"
android:src="@mipmap/icon_play"
android:layout_toLeftOf="@+id/local_music_bottom_iv_next"
android:layout_marginRight="20dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/local_music_bottom_iv_last"
android:layout_centerVertical="true"
android:src="@mipmap/icon_last"
android:layout_toLeftOf="@+id/local_music_bottom_iv_play"
android:layout_marginRight="20dp"/>
</RelativeLayout>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/local_music_rv"
android:layout_above="@+id/local_music_bottomlayout">
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
第二步item-local-music.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
app:contentPadding="10dp"
app:cardCornerRadius="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/item_local_music_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_centerVertical="true"
android:textSize="24sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/item_local_music_song"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@id/item_local_music_num"
android:text=""
android:textSize="18sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/item_local_music_singer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text=""
android:layout_below="@+id/item_local_music_song"
android:layout_alignLeft="@+id/item_local_music_song"
android:layout_marginTop="10dp"
android:textSize="14sp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item_local_music_album"
android:text=""
android:layout_toRightOf="@+id/item_local_music_singer"
android:layout_alignTop="@+id/item_local_music_singer"
android:textSize="14sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item_local_music_durtion"
android:layout_below="@+id/item_local_music_singer"
android:layout_alignParentRight="true"
android:text="3:52"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
第三步 权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
第四步
package com.example.mymusic02;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.ContentResolver;
import android.database.Cursor;
import android.media.MediaParser;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class MainActivity extends AppCompatActivity implements OnClickListener {
ImageView nextIv,playIv,lastIv;
TextView singerTv,songTv;
RecyclerView musicRv;
List<LocalMusicBean> mDatas;
private LocalMusicAdater adapter;
private int position;
int currnetPalyPosition=-1;
int currentPausePositionInSong=0;
MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
mediaPlayer=new MediaPlayer();
mDatas=new ArrayList<>();//z这里有两个选择
//创建适配器
adapter=new LocalMusicAdater(this,mDatas);
musicRv.setAdapter(adapter);
//s设置布局管理器
LinearLayoutManager layoutManager=new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
musicRv.setLayoutManager(layoutManager);
//加载本地数据源
loadLocalMusicData();
//设置点击监听事件
setEventListener();
}
private void setEventListener() {
adapter.setOnItemClickListener(new LocalMusicAdater.OnItemClickListener() {
@Override
public void OnItemClick(View view, int position) {
currnetPalyPosition=position;
LocalMusicBean musicBean=mDatas.get(position);
playMusicInMusicBean(musicBean);
}
});
}
public void playMusicInMusicBean(LocalMusicBean musicBean) {
//she设置歌曲名 歌手名字
singerTv.setText(musicBean.getSinger());
songTv.setText(musicBean.getSong());
stopMusic();
//chongz重置多媒体播放器
mediaPlayer.reset();
try {
mediaPlayer.setDataSource(musicBean.getPath());
palyMusic();
} catch (IOException e) {
e.printStackTrace();
}
}
private void palyMusic() {
if(mediaPlayer!=null&&!mediaPlayer.isPlaying()){
if(currentPausePositionInSong==0){
try {
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
}else {
//zha站厅到
mediaPlayer.seekTo(currentPausePositionInSong);
mediaPlayer.start();
}
playIv.setImageResource(R.mipmap.icon_pause);
}
}
private void pauseMusic() {
if(mediaPlayer!=null&&mediaPlayer.isPlaying()){
currentPausePositionInSong=mediaPlayer.getCurrentPosition();
mediaPlayer.pause();
playIv.setImageResource(R.mipmap.icon_play);
}
}
private void stopMusic() {
if(mediaPlayer!=null){
currentPausePositionInSong=0;
mediaPlayer.pause();
mediaPlayer.seekTo(0);
mediaPlayer.stop();
playIv.setImageResource(R.mipmap.icon_play);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
stopMusic();
}
private void loadLocalMusicData() {
//加载本地Mp3 文件
// 获取contentResolver对象
ContentResolver resolver=getContentResolver();
//获取本地音乐存储urid
Uri uri= MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cursor=resolver.query(uri,null,null,null,null);
int id=0;
while(cursor.moveToNext()){
String song = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
String singer=cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String album=cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
id++;
String sid=String.valueOf(id);
String path=cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
long duration=cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
SimpleDateFormat sdf=new SimpleDateFormat("mm:ss");
String time = sdf.format(new Date(duration));
LocalMusicBean bean=new LocalMusicBean(sid,song,singer,album,time,path);
mDatas.add(bean);
}
adapter.notifyDataSetChanged();
}
private void initView() {
nextIv=findViewById(R.id.local_music_bottom_iv_next);
playIv=findViewById(R.id.local_music_bottom_iv_play);
lastIv=findViewById(R.id.local_music_bottom_iv_last);
singerTv=findViewById(R.id.local_music_bottom_tv_singer);
songTv=findViewById(R.id.local_music_bottom_tv_song);
musicRv=findViewById(R.id.local_music_rv);
nextIv.setOnClickListener(this);
lastIv.setOnClickListener(this);
playIv.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.local_music_bottom_iv_last:
if(currnetPalyPosition==0){
Toast.makeText(this,"已经是第一首歌曲。",Toast.LENGTH_SHORT).show();
}
currnetPalyPosition=currnetPalyPosition-1;
LocalMusicBean lastBean=mDatas.get(currnetPalyPosition);
playMusicInMusicBean(lastBean);
break;
case R.id.local_music_bottom_iv_next:
if(currnetPalyPosition==mDatas.size()-1){
Toast.makeText(this,"next Music...",Toast.LENGTH_SHORT).show();
}
currnetPalyPosition=currnetPalyPosition+1;
LocalMusicBean nextBean=mDatas.get(currnetPalyPosition);
playMusicInMusicBean(nextBean);
break;
case R.id.local_music_bottom_iv_play:
if(currnetPalyPosition==-1){
Toast.makeText(this, "last Music...", Toast.LENGTH_SHORT).show();
return;
}
if(mediaPlayer.isPlaying()){
pauseMusic();
}else {
palyMusic();
}
break;
}
}
}
第五步LocalMusicAdater.java
package com.example.mymusic02;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
class LocalMusicAdater extends RecyclerView.Adapter<LocalMusicAdater.LocalMusicViewHolder>{
Context context;
List<LocalMusicBean> mDatas;
OnItemClickListener onItemClickListener;
public void setOnItemClickListener(OnItemClickListener onItemClickListener){
this.onItemClickListener=onItemClickListener;
}
public interface OnItemClickListener{
public void OnItemClick(View view,int position);
}
public LocalMusicAdater(Context context, List<LocalMusicBean> mDatas) {
this.context = context;
this.mDatas = mDatas;
}
@NonNull
@Override
public LocalMusicViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(context).inflate(R.layout.item_local_music,parent,false);
LocalMusicViewHolder holder=new LocalMusicViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull LocalMusicViewHolder holder, final int position) {
LocalMusicBean musicBean=mDatas.get(position);
holder.idTv.setText(musicBean.getId());
holder.songTv.setText(musicBean.getSong());
holder.singerTv.setText(musicBean.getSinger());
holder.albumTv.setText(musicBean.getAlbum());
holder.timeTv.setText(musicBean.getDuration());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onItemClickListener.OnItemClick(view,position);
}
});
}
@Override
public int getItemCount() {
return mDatas.size();
}
class LocalMusicViewHolder extends RecyclerView.ViewHolder{
TextView idTv,songTv,singerTv,albumTv,timeTv;
public LocalMusicViewHolder(@NonNull View itemView) {
super(itemView);
idTv=itemView.findViewById(R.id.item_local_music_num);
songTv=itemView.findViewById(R.id.item_local_music_song);
singerTv=itemView.findViewById(R.id.item_local_music_singer);
albumTv=itemView.findViewById(R.id.item_local_music_album);
timeTv=itemView.findViewById(R.id.item_local_music_durtion);
}
}
}
第六步 LocalMusicBean.java
package com.example.mymusic02;
public class LocalMusicBean {
private String id;
private String song;
private String singer;
private String album;
private String duration;
private String path;
public LocalMusicBean() {
}
public LocalMusicBean(String id, String song, String singer, String album, String duration, String path) {
this.id = id;
this.song = song;
this.singer = singer;
this.album = album;
this.duration = duration;
this.path = path;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSong() {
return song;
}
public void setSong(String song) {
this.song = song;
}
public String getSinger() {
return singer;
}
public void setSinger(String singer) {
this.singer = singer;
}
public String getAlbum() {
return album;
}
public void setAlbum(String album) {
this.album = album;
}
public String getDuration() {
return duration;
}
public void setDuration(String duration) {
this.duration = duration;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
![请添加图片描述](https://img-blog.csdnimg.cn/20200807023411442.png)
4444