1.MusicMedia 类
public class MusicMedia {
private int id; //音乐id
private String title; //音乐标题
private String artist;//音乐作者
private String url; //音乐存放路径
private String time; //音乐时长
private String size; //音乐大小
private int albumId; //专辑id
private String album; //专辑
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getTime() {
return time;
}
//修改时间格式
public void setTime(int time) {
time /= 1000;
int minute = time / 60;
int hour = minute / 60;
int second = time % 60;
minute %= 60;
this.time = String.format("%02d:%02d", minute, second);
}
public String getSize() {
return size;
}
public void setSize(Long size) {
long kb = 1024;
long mb = kb * 1024;
long gb = mb * 1024;
if (size >= gb) {
this.size = String.format("%.1f GB", (float) size / gb);
} else if (size >= mb) {
float f = (float) size / mb;
this.size = String.format(f > 100 ? "%.0f MB" : "%.1f MB", f);
} else if (size >= kb) {
float f = (float) size / kb;
this.size = String.format(f > 100 ? "%.0f KB" : "%.1f KB", f);
} else
this.size = String.format("%d B", size);
}
public int getAlbumId() {
return albumId;
}
public void setAlbumId(int albumId) {
this.albumId = albumId;
}
public String getAlbum() {
return album;
}
public void setAlbum(String album) {
this.album = album;
}
}
2.Main4Activity代码
public class Main4Activity extends AppCompatActivity {
private ListView listView;
private List list=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
listView= (ListView) findViewById(R.id.listView);
getList();
listView.setAdapter(new MyAdapter());
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
Intent intent=new Intent(Main4Activity.this,Main3Activity.class);
intent.putExtra("url",list.get(position).getUrl());
startActivity(intent);
}
});
}
public void getList(){
/*加载本地音乐文件库需要加READ_EXTERNAL_STORAGE权限
查询媒体数据库的uri
视频:MediaStore.Video.Media.EXTERNAL_CONTENT_URI
图片:MediaStore.Images.Media.EXTERNAL_CONTENT_URI
音乐(SD卡):MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
音乐(手机内部):MediaStore.Audio.Media.INTERNAL_CONTENT_URI
*/
ContentResolver contentResolver=getContentResolver();
Cursor cursor= contentResolver.query(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,null,null,null,MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
while(cursor!=null){
while (cursor.moveToNext()){
int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
//歌曲标题
String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
//歌曲的专辑名:MediaStore.Audio.Media.ALBUM
String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
int albumId = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
//歌曲的歌手名: MediaStore.Audio.Media.ARTIST
String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
//歌曲文件的路径 :MediaStore.Audio.Media.DATA
String url = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
//歌曲的总播放时长 :MediaStore.Audio.Media.DURATION
int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
//歌曲文件的大小 :MediaStore.Audio.Media.SIZE
Long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));
MusicMedia musicMedia = new MusicMedia();
musicMedia.setId(id);
musicMedia.setArtist(artist);
musicMedia.setSize(size);
musicMedia.setTitle(title);
musicMedia.setTime(duration);
musicMedia.setUrl(url);
musicMedia.setAlbum(album);
musicMedia.setAlbumId(albumId);
list.add(musicMedia);
}
}
}
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView ;
if(convertView==null){
textView = new TextView(Main4Activity.this);
}else{
textView = (TextView) convertView;
}
textView.setText(list.get(position).getTitle());
return textView;
}
}
}
3.Main3Activity代码 跳转播放页面代码
public class Main3Activity extends AppCompatActivity {
private Button start,end,pause;
MediaPlayer mediaPlayer;
private SeekBar seekBar;
Timer timer = new Timer();;
TimerTask timerTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
start = (Button) findViewById(R.id.start);
end = (Button) findViewById(R.id.end);
pause = (Button) findViewById(R.id.pause);
seekBar = (SeekBar) findViewById(R.id.seekBar);
Intent intent = getIntent();
String url = intent.getStringExtra("url");
mediaPlayer = MediaPlayer.create(Main3Activity.this, Uri.parse(url));
//歌曲的播放时长
seekBar.setMax(mediaPlayer.getDuration());
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Log.i("messgae","start");
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Log.i("messgae","stop");
//设置歌曲的进度为进度条的位置
mediaPlayer.seekTo(seekBar.getProgress());
}
});
}
public void click(View view){
int id = view.getId();
switch(id){
case R.id.start:
start();
break;
case R.id.end:
stop();
break;
case R.id.pause:
pause();
break;
}
}
public void start(){
mediaPlayer.start();
timerTask = new TimerTask() {
@Override
public void run() {
//歌曲当前播放位置
seekBar.setProgress(mediaPlayer.getCurrentPosition());
}
};
//1.等多久再执行什么
//2.每隔多久执行一次什么
timer.schedule(timerTask,0,100);
start.setEnabled(false);
}
public void pause(){
String pause1 = pause.getText().toString().trim();
if("暂停".equals(pause1)){
mediaPlayer.pause();
pause.setText("继续");
}else{
mediaPlayer.start();
pause.setText("暂停");
}
}
public void stop(){
if(mediaPlayer!=null){
timer.cancel();
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
start.setEnabled(true);
}
}
}
注:调试时,需要真机!!因为用的手机默认音乐