音乐播放器通常需要获取歌曲的专辑、作者、标题、年代等信息,将这些信息显示到UI界面上。
1、一种方式:解析媒体文件
命名空间:android.media.MediaMetadataRetriever
android提供统一的接口MediaMetadataRetriever解析媒体文件、获取媒体文件中取得帧和元数据(视频/音频包含的标题、格式、艺术家等信息)。
1 MediaMetadataRetriever mmr = new MediaMetadataRetriever(); 2 String str = getExternalStorageDirectory() + "music/hetangyuese.mp3"; 3 mmr.setDataSource(str); 4 // api level 10, 即从GB2.3.3开始有此功能 5 String title = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE); 6 // 专辑名 7 String album = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM); 8 // 媒体格式 9 String mime = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_MIMETYPE); 10 // 艺术家 11 String artist = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST); 12 // 播放时长单位为毫秒 13 String duration = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION); 14 // 从api level 14才有,即从ICS4.0才有此功能 15 String bitrate = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_BITRATE); 16 // 路径 17 String date = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DATE);
(1)关于视频缩略图:
2.2 之后:因为用了ThumbnailUtils类:
1 Bitmap b = ThumbnailUtils.createVideoThumbnail(path,Video.Thumbnails.MICRO_KIND); 2 ImageView iv = new ImageView(this);
2.2 之前:
使用MediaMetadataRetriever这个类;还可以通过getFrameAtTime方法取得指定time位置的Bitmap,即可以实现抓图(包括缩略图)功能
但是 里面有个问题
1.0之后 这个类被隐藏了 貌似2.3之后这个类又出现了,但是还可以直接copy源码进去使用:http://blog.csdn.net/lostinai/article/details/7734699 (教程)
1 // 对于视频,取第一帧作为缩略图,也就是怎样从filePath得到一个Bitmap对象。 2 private Bitmap createVideoThumbnail(String filePath) { 3 Bitmap bitmap = null; 4 MediaMetadataRetriever retriever = new MediaMetadataRetriever(); 5 try { 6 retriever.setMode(MediaMetadataRetriever.MODE_CAPTURE_FRAME_ONLY); 7 retriever.setDataSource(filePath); 8 bitmap = retriever.captureFrame(); 9 } catch(IllegalArgumentException ex) { 10 // Assume this is a corrupt video file 11 } catch (RuntimeException ex) { 12 // Assume this is a corrupt video file. 13 } finally { 14 try { 15 retriever.release(); 16 } catch (RuntimeException ex) { 17 // Ignore failures while cleaning up. 18 } 19 } 20 return bitmap; 21 }
(2)关于音乐缩略图:
1 // 对于音乐,取得AlbumImage作为缩略图,还是用MediaMetadataRetriever 2 private Bitmap createAlbumThumbnail(String filePath) { 3 Bitmap bitmap = null; 4 MediaMetadataRetriever retriever = new MediaMetadataRetriever(); 5 try { 6 retriever.setMode(MediaMetadataRetriever.MODE_GET_METADATA_ONLY); 7 retriever.setDataSource(filePath); 8 byte[] art = retriever.extractAlbumArt(); 9 bitmap = BitmapFactory.decodeByteArray(art, 0, art.length); 10 } catch(IllegalArgumentException ex) { 11 } catch (RuntimeException ex) { 12 } finally { 13 try { 14 retriever.release(); 15 } catch (RuntimeException ex) { 16 // Ignore failures while cleaning up. 17 } 18 } 19 return bitmap; 20 }
注意:直接得到Bitmap对象,把图片缩小到合适大小就OK。
这样获取出来的,信息有时候可能有乱码:
1、Java如何获取文件编码格式 (使用第三方库进行处理)
2、自己判断字符串是否有乱码,然后自行处理。
1 private boolean isMessyCode(String strName) { 2 try { 3 Pattern p = Pattern.compile("\\s*|\t*|\r*|\n*"); 4 Matcher m = p.matcher(strName); 5 String after = m.replaceAll(""); 6 String temp = after.replaceAll("\\p{P}", ""); 7 char[] ch = temp.trim().toCharArray(); 8 9 int length = (ch != null) ? ch.length : 0; 10 for (int i = 0; i < length; i++) { 11 char c = ch[i]; 12 if (!Character.isLetterOrDigit(c)) { 13 String str = "" + ch[i]; 14 if (!str.matches("[\u4e00-\u9fa5]+")) { 15 return true; 16 } 17 } 18 } 19 } catch (Exception e) { 20 e.printStackTrace(); 21 } 22 23 return false; 24 }
检查字符串是否乱码,乱码情况下要做相应的处理
2、二种方式:读取媒体文件数据库(避免乱码)
创建java bean:
1 public class MediaInfo { 2 private int playDuration = 0; 3 private String mediaName = ""; 4 private String mediaAlbum = ""; 5 private String mediaArtist = ""; 6 private String mediaYear = ""; 7 private String mFileName = ""; 8 private String mFileType = ""; 9 private String mFileSize = ""; 10 private String mFilePath = ""; 11 12 public Bitmap getmBitmap() { 13 return mBitmap; 14 } 15 16 public void setmBitmap(Bitmap mBitmap) { 17 this.mBitmap = mBitmap; 18 } 19 20 private Bitmap mBitmap = null; 21 22 public String getMediaName() { 23 return mediaName; 24 } 25 26 public void setMediaName(String mediaName) { 27 try { 28 mediaName =new String (mediaName.getBytes("ISO-8859-1"),"GBK"); 29 } catch (UnsupportedEncodingException e) { 30 // TODO Auto-generated catch block 31 e.printStackTrace(); 32 } 33 this.mediaName = mediaName; 34 } 35 36 public String getMediaAlbum() { 37 return mediaAlbum; 38 } 39 40 public void setMediaAlbum(String mediaAlbum) { 41 try { 42 mediaAlbum =new String (mediaAlbum.getBytes("ISO-8859-1"),"GBK"); 43 } catch (UnsupportedEncodingException e) { 44 // TODO Auto-generated catch block 45 e.printStackTrace(); 46 } 47 this.mediaAlbum = mediaAlbum; 48 } 49 50 public String getMediaArtist() { 51 return mediaArtist; 52 } 53 54 public void setMediaArtist(String mediaArtist) { 55 try { 56 mediaArtist =new String (mediaArtist.getBytes("ISO-8859-1"),"GBK"); 57 } catch (UnsupportedEncodingException e) { 58 // TODO Auto-generated catch block 59 e.printStackTrace(); 60 } 61 this.mediaArtist = mediaArtist; 62 } 63 }
媒体信息bean
操作类:
1 public class MediaInfoProvider { 2 3 /** 4 * context 5 */ 6 private Context mContext = null; 7 8 /** 9 * data path 10 */ 11 private static final String dataPath = "/mnt"; 12 13 /** 14 * query column 15 */ 16 private static final String[] mCursorCols = new String[] { 17 MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME, 18 MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DURATION, 19 MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM, 20 MediaStore.Audio.Media.YEAR, MediaStore.Audio.Media.MIME_TYPE, 21 MediaStore.Audio.Media.SIZE, MediaStore.Audio.Media.DATA }; 22 23 /** 24 * MediaInfoProvider 25 * @param context 26 */ 27 public MediaInfoProvider(Context context) { 28 this.mContext = context; 29 } 30 31 /** 32 * get the media file info by path 33 * @param filePath 34 * @return 35 */ 36 public MediaInfo getMediaInfo(String filePath) { 37 38 /* check a exit file */ 39 File file = new File(filePath); 40 if (file.exists()) { 41 Toast.makeText(mContext, "sorry, the file is not exit!", 42 Toast.LENGTH_SHORT); 43 } 44 45 /* create the query URI, where, selectionArgs */ 46 Uri Media_URI = null; 47 String where = null; 48 String selectionArgs[] = null; 49 50 if (filePath.startsWith("content://media/")) { 51 /* content type path */ 52 Media_URI = Uri.parse(filePath); 53 where = null; 54 selectionArgs = null; 55 } else { 56 /* external file path */ 57 if(filePath.indexOf(dataPath) < 0) { 58 filePath = dataPath + filePath; 59 } 60 Media_URI = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 61 where = MediaColumns.DATA + "=?"; 62 selectionArgs = new String[] { filePath }; 63 } 64 65 /* 查询 */ 66 Cursor cursor = mContext.getContentResolver().query(Media_URI, 67 mCursorCols, where, selectionArgs, null); 68 if (cursor == null || cursor.getCount() == 0) { 69 return null; 70 } else { 71 cursor.moveToFirst(); 72 MediaInfo info = getInfoFromCursor(cursor); 73 printInfo(info); 74 return info; 75 } 76 } 77 78 /** 79 * 获取媒体信息 80 * @param cursor 81 * @return 82 */ 83 private MediaInfo getInfoFromCursor(Cursor cursor) { 84 MediaInfo info = new MediaInfo(); 85 86 /* file name */ 87 if(cursor.getString(1) != null) { 88 info.setmFileName(cursor.getString(1)); 89 } 90 /* media name */ 91 if(cursor.getString(2) != null) { 92 info.setMediaName(cursor.getString(2)); 93 } 94 /* play duration */ 95 if(cursor.getString(3) != null) { 96 info.setPlayDuration(cursor.getInt(3)); 97 } 98 /* artist */ 99 if(cursor.getString(4) != null) { 100 info.setMediaArtist(cursor.getString(4)); 101 } 102 /* album */ 103 if(cursor.getString(5) != null) { 104 info.setMediaAlbum(cursor.getString(5)); 105 } 106 /* media year */ 107 if (cursor.getString(6) != null) { 108 info.setMediaYear(cursor.getString(6)); 109 } else { 110 info.setMediaYear("undefine"); 111 } 112 /* media type */ 113 if(cursor.getString(7) != null) { 114 info.setmFileType(cursor.getString(7).trim()); 115 } 116 /* media size */ 117 if (cursor.getString(8) != null) { 118 float temp = cursor.getInt(8) / 1024f / 1024f; 119 String sizeStr = (temp + "").substring(0, 4); 120 info.setmFileSize(sizeStr + "M"); 121 } else { 122 info.setmFileSize("undefine"); 123 } 124 /* media file path */ 125 if (cursor.getString(9) != null) { 126 info.setmFilePath(cursor.getString(9)); 127 } 128 129 return info; 130 } 131 }
时间: 2024-10-07 13:32:10