Android Development: Scan local music files

Source: Internet
Author: User
Tags sqlite database

I. Preface

Originally thought so easy, really did not think, also engaged in long time, make me sweat, to shoot table fell keyboard.
The features that you want to implement are:
Through the NetEase cloud Music/Baidu Music/qq Music/Cool dog music in an API, through the keyword/singer/lyrics to search for relevant song information, and then play online, download to local, but for the time being, the use of the API is still a bit of a problem. There is little problem with browser access, for example:

But want to access through the mobile phone, the problem arises, the same parameters, the results are very different, and the basic search is not what they want, and later to do through their own server to visit, or search, and then checked the information, and temporarily did not solve. So, now just by scanning QQ music has been downloaded songs, to achieve the playback function. In this blog post, I first recorded the scan music files, as for the play and download, after the back if you have organized, and then posted out.
About NetEase Cloud Music/Baidu Music/qq Music/Cool Dog Music API use, have friends if know for Android, request recommended!

Two. Scan local music files

First through the QQ music has downloaded the good song.
Scan implementation:

//scan local musicif(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {NewThread () {@Override         Public void Run()        {Try{string[] ext = {". mp3"}; File File =NewFile (environment.getexternalstoragedirectory () +"/android/data/com.tencent.qqmusic/files/qqmusic/song");//Can be "//////" This string is removed, sweep surface range is the entire SD card, I am too slow, and sweep out a lot ... is not the song that I want, finally or directly written to the download directory of QQ music, in fact, their app support download, you can download the designated directory of songs, and then scan the designated directory on the line ~ The global scan is really not appropriate, not antivirus.Localmusiclist = Common.searchmp3infos (File,ext);            Hander.sendemptymessage (search_music_success); }Catch(Exception e) {}}}.start ();}

Here is the code for Common.searchmp3infos ()

//Scan implementation    Staticlist<string> list =NewArraylist<> (); Public StaticList<string>Searchmp3infos(file file, string[] ext) {if(File! =NULL) {if(File.isdirectory ()) {file[] listfile = File.listfiles ();if(ListFile! =NULL) { for(inti =0; i < listfile.length;                    i++) {Searchmp3infos (Listfile[i], ext); }                }            }Else{String filename = File.getabsolutepath (); for(inti =0; i < ext.length; i++) {if(Filename.endswith (Ext[i])) {list.add (filename); Break; }                }            }        }returnList }

Then in the handler through the adapter in the ListView display, the effect is simple like this, or that sentence, do not despise it ugly:

Well, sweep it out first, show the problem and change it ~ ~ ~

Another method, the test of their own temporarily did not sweep out, that is, from the SQLite database sweeping music files, a lot of friends are mentioned, but do not know where their problems, can only sweep to the recording file ~ I also posted out for reference.

public static list. Getcontentresolver(). Query(Mediastore. Audio. Media. EXTERNAL_content_uri, NULL, NULL, NULL, or NULL);ArrayList Mp3infos = new ArrayList ();for (int i =0; i < Cursor.getcount (); i++) {Cursor. MoveToNext();hashmap<string, object> musicmap = new Hashmap<> ();Long id = cursor. Getlong(cursor. Getcolumnindex(Mediastore. Audio. Media. _id));//Music IDString title = cursor. getString(cursor. Getcolumnindex(Mediastore. Audio. Media. TITLE));//Music titleString artist = cursor. getString(cursor. Getcolumnindex(Mediastore. Audio. Media. ARTIST));//ArtistString album = cursor. getString(cursor. Getcolumnindex(Mediastore. Audio. Media. ALBUM));//albumsLong albumID = cursor. Getlong(cursor. Getcolumnindex(Mediastore. Audio. Media. ALBUM_ID));//album IDLong Duration = cursor. Getlong(cursor. Getcolumnindex(Mediastore. Audio. Media. DURATION));//Time lengthLong size = cursor. Getlong(cursor. Getcolumnindex(Mediastore. Audio. Media. SIZE));//File SizeString URL = cursor. getString(cursor. Getcolumnindex(Mediastore. Audio. Media. DATA));//file pathint ismusic = cursor. GetInt(cursor. Getcolumnindex(Mediastore. Audio. Media. is_music));//Whether for musicif (ismusic! =0) {Musicmap. Put("id", id);Musicmap. Put("title", title);Musicmap. Put("Artist", artist);Musicmap. Put("album", album);Musicmap. Put("albumID", albumID);Musicmap. Put("duration", duration);Musicmap. Put("Size", size);Musicmap. Put("url", URL);Musicmap. Put("Ismusic", Ismusic);Mp3infos. Add(Musicmap);}} Cursor. Close();Return Mp3infos;}

Android Development: Scan local music files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.