标签:
1.内容提供者查找手机SD卡下的音乐
配置读取SD卡权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
//调用方法
List<Good> list = GetMusic.Music(MainActivity.this);
package com.bawei.day07_music;
import java.util.ArrayList;
import com.bawei.vo.Good;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio;
public class GetMusic {
public static ArrayList<Good> Music(Context context) {
ContentResolver resolver = context.getContentResolver();
// 内容提供者访问MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
Cursor cursor = resolver.query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
null);
ArrayList<Good> list=new ArrayList<Good>();
while (cursor.moveToNext()) {
// 名字
String name = cursor.getString(cursor
.getColumnIndex(Audio.Media.TITLE));
// 专辑名
String album = cursor.getString(cursor
.getColumnIndexOrThrow(Audio.Media.ALBUM));
// 歌手名
String artist = cursor.getString(cursor
.getColumnIndexOrThrow(Audio.Media.ARTIST));
// URI歌曲文件存放路径
String path = cursor.getString(cursor
.getColumnIndexOrThrow(Audio.Media.DATA));
// 歌曲文件播放时间长度
int duration = cursor.getInt(cursor
.getColumnIndexOrThrow(Audio.Media.DURATION));
// 音乐文件大小
int size = cursor.getInt(cursor
.getColumnIndexOrThrow(Audio.Media.SIZE));
//保存到集合
list.add(new Good(name,artist,path,false));
}
return list;
}
}
2.播放音乐
//播放音乐
private void startMusic(int arg0) {
//让上一首停下
if (player != null && player.isPlaying()) {
player.stop();
player.reset();
player = null;
}
try {
//播放器
player = new MediaPlayer();
//播放路径
player.setDataSource(list.get(arg0).getMusic());
//准备
player.prepare();
//播放
player.start();
//得到歌曲总进度
c = player.getDuration();
//给进度条seekbar设置最大值
seekBar.setMax(c);
//timer
time();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//timer
private void time() {
// TODO Auto-generated method stub
//timer
timer = new Timer();
//播放进度100毫秒前进一下
timer.schedule(new TimerTask() {
private int d;
@Override
public void run() {
// TODO Auto-generated method stub
//得到当前播放进度
d = player.getCurrentPosition();
//赋给seekbar
seekBar.setProgress(d);
//handler传值 显示时间
Message message = Message.obtain();
message.arg1 = c;
message.arg2 = d;
handler.sendMessage(message);
}
}, 0, 100);
}
//显示时间
Handler handler = new Handler() {
public void handleMessage(Message msg) {
//当前进度
int cu1 = msg.arg1;
//总长度
int cu2 = msg.arg2;
//转换成时间格式
SimpleDateFormat sim = new SimpleDateFormat("mm:ss");
String cur1 = sim.format(cu1);
String cur2 = sim.format(cu2);
//赋给textview
text1.setText(cur2);
text2.setText(cur1);
};
};
3.暂停继续播放
if (player != null) {
//音乐暂停
player.pause();
//得到当前seekBar进度
m = seekBar.getProgress();
}
//继续播放
player.seekTo(m);
4.seekbar调整音乐播放进度
//seekbar调整音乐播放进度
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
if (player != null) {
//得到当前拖拽的进度
player.seekTo(seekBar.getProgress());
//timer
timecan();
//timer计时
time();
}
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
//清空timer
timecan();
}
public void onProgressChanged(SeekBar seekBar,
int progress, boolean fromUser) {
// TODO Auto-generated method stub
}
});
////清空timer
private void timecan() {
// TODO Auto-generated method stub
if (timer != null) {
timer.cancel();
}
}
5.Intent跳转传list集合
先把List<>改为ArrayList<>
ArrayList<Good> list=new ArrayList<Good>();
Intent intent = new Intent(MainActivity.this,InfoActivity.class);
intent.putExtra("list", list);
startActivity(intent);
Intent intent = getIntent();
ArrayList<Good> list= (ArrayList<Good>) intent.getSerializableExtra("list");
标签:
原文地址:http://www.cnblogs.com/wbp0818/p/5401803.html