标签:
本文出自:http://blog.csdn.net/dt235201314/article/details/51341078
一丶本地音乐加载相当于就是listVIew应用
扣丁音乐1.0前部分(gif图大小限制)演示:
实体类Mp3Info(歌曲相关数据及get和set方法)
public class Mp3Info { private long id; private String title;//歌名 private String artist;//艺术家 private String album;//专辑 private long albumId; private long duration;//时长 private long size;//大小 private String url;//路径 private int isMusic;//是否为音乐 public long getId() { return id; } public void setId(long 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 getAlbum() { return album; } public void setAlbum(String album) { this.album = album; } public long getAlbumId() { return albumId; } public void setAlbumId(long albumId) { this.albumId = albumId; } public long getDuration() { return duration; } public void setDuration(long duration) { this.duration = duration; } public long getSize() { return size; } public void setSize(long size) { this.size = size; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public int getIsMusic() { return isMusic; } public void setIsMusic(int isMusic) { this.isMusic = isMusic; } @Override public String toString() { return "Mp3Info{" + "id=" + id + ", title=‘" + title + ‘\‘‘ + ", artist=‘" + artist + ‘\‘‘ + ", album=‘" + album + ‘\‘‘ + ", albumId=" + albumId + ", duration=" + duration + ", size=" + size + ", url=‘" + url + ‘\‘‘ + ", isMusic=" + isMusic + ‘}‘; } }工具类MediaUtils(实现例如筛选歌曲长度,时间格式化,图片处理等)
public class MediaUtils { //获取专辑封面的Uri private static final Uri albumArtUri = Uri.parse("content://media/external/audio/albumart"); /** * 获取默认专辑图片 */ public static Bitmap getDefaultArtwork(Context context,boolean small){ BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inPreferredConfig = Bitmap.Config.RGB_565; if(small){ return BitmapFactory.decodeStream(context.getResources().openRawResource(R.drawable.music_play),null,opts); } return BitmapFactory.decodeStream(context.getResources().openRawResource(R.drawable.music_play),null,opts); } /** * 从文件当中获取专辑封面位图 */ private static Bitmap getArtworkFromFile(Context context,long songid,long albumid){ Bitmap bm = null; if(albumid<0 && songid<0){ throw new IllegalArgumentException("Must specify an album or a song id"); } try { BitmapFactory.Options options = new BitmapFactory.Options(); FileDescriptor fd = null; if(albumid<0){ Uri uri = Uri.parse("content://media/external/audio/media" +songid+"albumart"); ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri,"r"); if(pfd!=null){ fd = pfd.getFileDescriptor(); } }else{ Uri uri = ContentUris.withAppendedId(albumArtUri,albumid); ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r"); if(pfd!=null){ fd = pfd.getFileDescriptor(); } } options.inSampleSize=1; //只进行大小判断 options.inJustDecodeBounds = true; //调用此方法得到options得到图片大小 BitmapFactory.decodeFileDescriptor(fd,null,options); //我们的目标是在800pixel的画面上显示 //所以需要调用computeSampleSize得到图片缩放的比例 options.inSampleSize = 100; //我们得到了缩放的比例,现在开始正式读入Bitmap数据 options.inJustDecodeBounds = false; options.inDither = false; options.inPreferredConfig = Bitmap.Config.ARGB_8888; //根据options参数,减少所需要的内存 bm = BitmapFactory.decodeFileDescriptor(fd,null,options); }catch (FileNotFoundException e){ e.printStackTrace(); } return bm; } /** * 获取专辑封面位图对象 */ public static Bitmap getArtwork(Context context,long song_id,long album_id,boolean allowdefault,boolean small){ if(album_id<0){ if(song_id<0){ Bitmap bm = getArtworkFromFile(context,song_id,-1); if(bm!=null){ return bm; } } if(allowdefault){ return getDefaultArtwork(context,small); } return null; } ContentResolver res = context.getContentResolver(); Uri uri = ContentUris.withAppendedId(albumArtUri,album_id); if(uri !=null){ InputStream in = null; try { in = res.openInputStream(uri); BitmapFactory.Options options = new BitmapFactory.Options(); //先指定原始大小 options.inSampleSize = 1; //只进行大小判断 options.inJustDecodeBounds = true; //调用此方法得到options得到图片的大小 BitmapFactory.decodeStream(in,null,options); //我们的目标是在你N pixel的画面上显示。所以需要调用computeSampleSize得到图片缩放的比例 //这里的target为800是根据默认专辑图片代傲决定的,800只是测试数字但是试验后发现完美的结合 if(small){ options.inSampleSize = computeSampleSize(options,40); }else { options.inSampleSize = computeSampleSize(options,600); } //我们得到了缩放比例,现在开始正式读入Bitmap数据 options.inJustDecodeBounds = false; options.inDither = false; options.inPreferredConfig = Bitmap.Config.ARGB_8888; in = res.openInputStream(uri); return BitmapFactory.decodeStream(in,null,options); }catch (FileNotFoundException e){ Bitmap bm = getArtworkFromFile(context,song_id,album_id); if(bm!=null){ if(bm.getConfig()==null){ bm = bm.copy(Bitmap.Config.RGB_565,false); if(bm == null && allowdefault){ return getDefaultArtwork(context,small); } }else if(allowdefault){ bm = getDefaultArtwork(context,small); } return bm; } }finally { try { if(in != null){ in.close(); } }catch (IOException e){ e.printStackTrace(); } } } return null; } /** * 对图片进行合适的缩放 */ public static int computeSampleSize(BitmapFactory.Options options,int target){ int w = options.outWidth; int h = options.outHeight; int candidateW = w / target; int candidateH = h / target; int candidate = Math.max(candidateW,candidateH); if(candidate == 0){ return 1; } if (candidate>1){ if((w>target)&&(w/candidate)<target){ candidate -= -1; } } if(candidate>1){ if((h>target)&&(h/candidate)<target){ candidate -= -1; } } return candidate; } /** * 用于从数据库中查询歌曲的信息,保存在List当中 */ public static ArrayList<Mp3Info> getMp3Infos(Context context){ Cursor cursor = context.getContentResolver().query( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, //最小音乐长度 MediaStore.Audio.Media.DURATION + ">=180000", null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER); ArrayList<Mp3Info> mp3Infos = new ArrayList<Mp3Info>(); for(int i=0;i<cursor.getCount();i++){ cursor.moveToNext(); Mp3Info mp3Info = new Mp3Info(); long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID));//id String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));//歌名 String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));//艺术家 String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));//专辑 long albumId = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));//专辑id long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));//时长 long size = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE));//大小 String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));//路径 int isMusic = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));//是否为音乐 if(isMusic!=0){ mp3Info.setId(id); mp3Info.setTitle(title); mp3Info.setArtist(artist); mp3Info.setAlbum(album); mp3Info.setAlbumId(albumId); mp3Info.setDuration(duration); mp3Info.setSize(size); mp3Info.setUrl(url); mp3Infos.add(mp3Info); } } cursor.close(); return mp3Infos; } /** * 格式化时间,将毫秒转换为分:秒格式 */ public static String formatTime(long time){ String min = time / (1000 * 60)+""; String sec = time % (1000 * 60)+""; if(min.length()<2){ min = "0"+time / (1000 * 60)+""; }else{ min = time / (1000 * 60)+""; } if(sec.length()==4){ sec = "0"+(time % (1000 * 60))+""; }else if(sec.length()==3){ sec = "00"+(time % (1000 * 60))+""; }else if(sec.length()==2){ sec = "000"+(time % (1000 * 60))+""; }else if(sec.length()==1){ sec = "0000"+(time % (1000 * 60))+""; } return min + ":" +sec.trim().substring(0,2); }}activity_local_songs.xml(后面贴出)
item_music_fragment.xml(略)
很明显这里我和视频显示得不一样,歌曲图片不好找显示出来也不是很美观,于是我显示的是歌名,歌手,专辑,时间这个xml应个人喜好可调整
LocalSongsListAdapter.java
public class LocalSongsListAdapter extends BaseAdapter{ private Context context; private ArrayList<Mp3Info> mp3Infos; public LocalSongsListAdapter(Context context,ArrayList<Mp3Info> mp3Infos){ this.context = context; this.mp3Infos = mp3Infos; } public void setMp3Infos(ArrayList<Mp3Info> mp3Infos){ this.mp3Infos = mp3Infos; } @Override public int getCount() { return mp3Infos.size(); } @Override public Object getItem(int position) { return mp3Infos.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder vh; if(convertView==null){ convertView = LayoutInflater.from(context).inflate(R.layout.item_music_list,null); vh = new ViewHolder(); vh.tv_song_name = (TextView)convertView.findViewById(R.id.tv_song_name); vh.tv_song_artist = (TextView)convertView.findViewById(R.id.tv_song_artist); vh.tv_song_album = (TextView)convertView.findViewById(R.id.tv_song_album); vh.tv_song_duration = (TextView)convertView.findViewById(R.id.tv_song_duration); convertView.setTag(vh); }else{ vh = (ViewHolder)convertView.getTag(); } //给控件赋值要写在if语句外面,否则第一次加载数据失败 Mp3Info mp3Info = mp3Infos.get(position); vh.tv_song_name.setText(mp3Info.getTitle()); vh.tv_song_artist.setText(mp3Info.getArtist()); vh.tv_song_album.setText(mp3Info.getAlbum()); vh.tv_song_duration.setText(MediaUtils.formatTime(mp3Info.getDuration())); return convertView; } static class ViewHolder{ TextView tv_song_name; TextView tv_song_artist; TextView tv_song_album; TextView tv_song_duration; } }LocalSongsActivity.java
写到这里LocalSongsActivity.java里面只需要初始化控件,后面再贴出
/** * 初始化本地音乐列表 */ private void initDate() { mp3Infos = MediaUtils.getMp3Infos(this); System.out.println(mp3Infos.size()); localSongsListAdapter = new LocalSongsListAdapter(this,mp3Infos); localSongsListAdapter.notifyDataSetChanged(); lv_local_songs_list.setAdapter(localSongsListAdapter); }
SD卡读取权限,网络权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.INTERNET"></uses-permission>
到这里就能完成歌曲的加载了
标签:
原文地址:http://blog.csdn.net/dt235201314/article/details/51341078