SimpleAdapter adapter = new SimpleAdapter(this, music_name,
R.layout.list, new String[] { "name", "time" }, new int[] {
R.id.musicName, R.id.musicTime });
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
music_count = arg2;
HashMap path = (HashMap) music_name.get(arg2);
String abc = (String) path.get("data");
C_time = (Long) path.get("time1");
binder.next(abc);
}
});
Cursor cursor = getContentResolver()
.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME }, null,
null, null);
// ↑↑↑↑↑↑↑ 0信息歌曲名,1时间2专辑名3歌手4ID 5路径6显示全部名字 可以区分什么格式比如.mp3 .wma
// ↓↓↓↓↓↓↓ 遍历cursor 把数据放如一个ArrayList里面,之后把该list放到adapter里面就可以显示了
if (cursor != null && cursor.moveToFirst()) {
do {
String mName = cursor.getString(0);
long mTime = cursor.getLong(1);
String mAlbum = cursor.getString(2);
String mArtist = cursor.getString(3);
long mId = cursor.getLong(4);
String mData = cursor.getString(5);
HashMap a = new HashMap();
a.put("name", mName);
a.put("time", refreshTotalTime(mTime));
a.put("time1", mTime);
a.put("album", mAlbum);
a.put("artist", mArtist);
a.put("id", mId);
a.put("data", mData);
music_name.add(a);//ArrayList
System.out.println(mName + "-->" + mTime + "---->>--" + mData);
} while (cursor.moveToNext());
}
if (!cursor.isClosed()) {
cursor.close();
}
本地音乐播放器MediaPlayer获取所有歌曲信息,getContentResolver()
最新推荐文章于 2023-02-13 19:00:05 发布