package com.music.zhangdaosen; import android.annotation.SuppressLint; import android.content.ContentResolver; import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.provider.MediaStore; import android.util.Log; import android.widget.ArrayAdapter; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; public class SongHelper { public static List<Song>musicList = new ArrayList<>(); // static List<Song>musicList; public static List<Song> getLocalMusic(Context context){ Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; ContentResolver resolver = context.getContentResolver(); Cursor cursor = resolver.query(uri,null,null,null,null); if(cursor !=null){ Song song; while (cursor.moveToNext()){ song =new Song(); int index = cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME); song.name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME)); song.singer = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)); song.path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)); song.duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)); Log.d("SongHelper--keyar","get"+song.name+" "+song.duration); musicList.add(song); SimpleDateFormat sdf = new SimpleDateFormat("mm:ss"); String time = sdf.format(new Date(song.duration)); } Log.d("SongHelper--keyar","get musicList"+ musicList.size()); } cursor.close(); return musicList; } public static String changeTime(int duration){ int min = duration/1000/60; int sec = duration/1000%60; if(sec<10){ return min+":0"+sec; } return min+":"+sec; } }
时间: 2024-04-27 17:24:06 浏览: 154
配置hibernate-distribution-3.6.6.Final时出现的问题及解决办法
这是一段Java代码,看起来是一个获取本地音乐列表的帮助类。它使用了Android提供的MediaStore来查询本地的音频文件,然后将查询到的结果封装成一个Song对象,并保存在一个List中。可以通过调用`getLocalMusic()`方法来获取本地音乐列表。其中,每一个Song对象代表一首音乐,包含了音乐的名称、歌手、路径、时长等信息。`changeTime()`方法用于将时间格式从毫秒转换为分秒形式。
阅读全文