In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
MediaStore player production example analysis, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
In the original idea, I used the File class to recursively iterate through all the files in the SD card, then filter out the files ending in the ".mp3" format and put them into a map. Then put map into List and let media load.
However, it is found that this method takes a long time and is prone to "jam". Then I want to save List permanently locally, but I find that if there are hundreds of songs, the file is too large and takes up a lot of memory control. After consulting some materials, it is found that android mobile phone itself has a media library, which can be queried directly. After many attempts, the file was read successfully. Take a note here.
First, get the ContextResolver instance
Method: ContexrResolver cr=context.getContextResolver ()
Explanation: context represents the context of Activity.this, which is the Context in the Activity to be operated.
When you use the getContextResolver (); method to get the ContextResolver object, you can query it directly.
Second, use the Cursor object
Methods:
Query (uri, projection, selection, selectionArgs, sortOrder)
The cr here is the ContextResolver object obtained earlier; in this way, the ContextResolver object must be instantiated first.
So, can you give an example directly? You can do it.
2 c=context.getContentResolver (). Query (uri, projection, selection, selectionArgs, sortOrder)
Just use context.getContextResolver () directly. The two ways can not go wrong, according to the actual situation, use different ways.
The meaning of the parameter:
Uri: indicate the name of the database to be queried plus the name of the table. The parameters of the corresponding information can be found in MediaStore. For more information, please refer to the development documentation.
Projection: specify which columns in the query database table, and the returned cursors will include the corresponding information. Null returns all the information.
Selection: specify query criteria
SelectionArgs: is it in the parameter selection? The symbol is, here you can replace the question mark with the actual value. What if selection doesn't have this? Then the String array can be null.
SortOrder: specifies the order in which query results are sorted
Third, MediaStore database
Main statement:
MediaStore.Audio: store audio information MediaStore.Image: store picture information MediaStoue.Video: store video information
These three are the three basic file information, and there are many methods and parameters after each class. At present, I am using audio information, other information has not been used, I think the method should be about the same.
1MediaStore.Audio query parameters:
The first thing we want to query is the media file, which is the Media class, so add: Media after the subject sentence
Turned into: MediaStore.Audio.Media
OK, now what we want to query is some information about the media. Here I have collected some commonly used attributes that you can refer to:
MediaStore.Audio.Media._ID; / / Internal ID
MediaStore.Audio.Media.DISPLAY_NAME;// Music name
MediaStore.Audio.Media.TITLE; / / Music title
MediaStore.Audio.Media.ARTIST; / / singer
MediaStore.Audio.Media.ALBUM; / / album to which it belongs
MediaStore.Audio.Media.DURATION; / / Music duration
MediaStore.Audio.Media.DATA; / / Music path
The above are some commonly used attributes when querying files. So how do we use these attributes?
I found two ways to inquire:
The first kind:
What I use here is to put the ContextResolver object directly in the statement.
When getting the duration of music, you should pay attention to it. The duration here is not changed. I don't know whether it is per second or millisecond. Anyway, it will print out a long string. So, we need to change:
Public class tools {/ * conversion time * * @ param time * @ return * / 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); }} 啊~不是我写的,网上抄来的.哈哈哈.可以看到在获取时长的那一段之前我就用了. 第一种方法,个人感觉比较清晰一点. public static List findMusicFileUseCuesor( Context context) { Cursor cursor = context.getContentResolver().query( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER); List list = new ArrayList(); if (cursor.moveToFirst()) { do { Map map = new HashMap(); // 获得音乐ID map.put("_id", cursor.getString( cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID)) .toString()); // 获得音乐名称 map.put("name", cursor.getString(cursor .getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME))); // 获得音乐标题 map.put("tittle", cursor.getString(cursor .getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE))); // 获得音乐歌手 map.put("artist", cursor.getString(cursor .getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST))); // 获得音乐专辑 map.put("album", cursor.getString(cursor .getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM))); // 获得音乐时长 map.put("duration", tools.formatTime(cursor.getLong(cursor .getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)))); // 获得音乐路径 map.put("path", cursor.getString(cursor .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA))); list.add(map); } while (cursor.moveToNext()); } if (list.size() >0) {for (int I = 0; I
< list.size(); i++) { System.out.println("list :---->"+ list.get (I) .toString ();} return list;} return null;}
The second method:
Public static List findMusicFileFromPhone (Context context) {Cursor cursor = context.getContentResolver (). Query (Parameter.MEDIA_URI, Parameter.MEDIA_PROJECTION, Parameter.MEDIA_SELECTION, Parameter.MEDIA_SELECTIONARGS, null); List musicDate = new ArrayList () While (cursor.moveToNext ()) {Map map = new HashMap (); map.put ("tittle", cursor.getString (0)); map.put ("artist", cursor.getString (1)); map.put ("durtion", tools.formatTime (cursor.getLong (2) Map.put ("path", cursor.getString (3)); musicDate.add (map);} if (musicDate.size () > 0) {return musicDate;} else {System.out.println ("musicDate:" + musicDate.size ()) Return null;}}
The parameters of the second method:
Public static final Uri MEDIA_URI = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;public static final String [] MEDIA_PROJECTION = {MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.DATA} Public static final String [] MEDIA_SELECTIONARGS = {"audio/mpeg", "audio/x-ms-wma"}; public static final String MEDIA_SELECTION = MediaStore.Audio.Media.MIME_TYPE+ "=? Or "+ MediaStore.Audio.Media.MIME_TYPE +" =? "
I can probably guess what the last two sentences mean here, but I still hope Daniel can explain.
Especially the last sentence, it should be the type of screening, read online and say "?" The place can be defined by myself, but as soon as I change this code, I will report an error. I don't know why. I have tried. MP3 and MP3 lowercase also tried. =. = ask for popular science.
In this way, I think, first, it looks rather messy. Well, actually, I didn't write it well. Second, I always feel that it is not good to write dead data when writing, such as:
Map.put ("tittle", cursor.getString (0))
If the order of the parameters is changed, it will be funny.
But this way is more convenient when updating, adding, deleting, and operating.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.