I want the music player application not to check the directories for audio files every time the application starts. How can i do this?

I want the music player application not to scan directories for audio files every time the application starts. How can i do this?

I used the following code to scan audio files.

public void getSongList() { ContentResolver contentResolver=getContentResolver(); Uri musicUri=android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; Cursor musicCursor = contentResolver.query(musicUri, null, null, null, null); if(musicCursor!=null && musicCursor.moveToFirst()) { //get columns int titleColumn = musicCursor.getColumnIndex (android.provider.MediaStore.Audio.Media.TITLE); int idColumn = musicCursor.getColumnIndex (android.provider.MediaStore.Audio.Media._ID); int artistColumn = musicCursor.getColumnIndex (android.provider.MediaStore.Audio.Media.ARTIST); int albumIDColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID); //add songs to list do { long thisId = musicCursor.getLong(idColumn); String thisTitle = musicCursor.getString(titleColumn); String thisArtist = musicCursor.getString(artistColumn); long thisAlbumID=musicCursor.getLong(albumIDColumn); Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart"); Bitmap albumArtBitMap=null; Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, thisAlbumID); try { albumArtBitMap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), albumArtUri); Matrix m = new Matrix(); m.setRectToRect(new RectF(0, 0, albumArtBitMap.getWidth(), albumArtBitMap.getHeight()), new RectF(0, 0, 300, 300), Matrix.ScaleToFit.CENTER); albumArtBitMap = Bitmap.createBitmap(albumArtBitMap, 0, 0, albumArtBitMap.getWidth(), albumArtBitMap.getHeight(), m, true); } catch (IOException e) { e.printStackTrace(); } songList.add(new Song(thisId, thisTitle, thisArtist,albumArtBitMap)); } while (musicCursor.moveToNext()); } } 

I want the application to be checked only when new files appear. Because if I scan the entire SD card every time, it will take too long to launch the application. Please help me with this.

+7
android audio android-mediaplayer music audio-player player
source share
3 answers

No need to leave all the songs in the list of local applications.

To show mp3list, you can use the limit of the list of content providers list adapter request (by page scroll request)

To perform a search, use the contentprovider request method.

Save only the playlist in the local database pointing to mp3 uri.

this link can help you: How to update the list whose data was requested from the database through SimpleCursorAdapter?

+1
source share

Each time you launch the application, see how much space you have on your device.

 File path = Environment.getDataDirectory(); megaBytesAvailable(path) public static float megaBytesAvailable(File file) { StatFs stat = new StatFs(file.getPath()); long bytesAvailable = (long)stat.getBlockSizeLong() * (long)stat.getAvailableBlocksLong(); return bytesAvailable / (1024.f * 1024.f); } 

Save it in the application cache as a variable and compare it every time you start the application, if it is more than you know what you need to scan.

 if(comparedVariable < megaBytesAvailable(music_directory_path)){ getSongList(); //Save it again to compare next time if more storage is used comparedVariable = megaBytesAvailable(music_directory_path); //Save it to SharedPrefs for next boot up comparison } 
+1
source share

I think the @royrok answer may help you, where @royrok checks the playlist in the mediarester, instead re-scans the sdcard. Below I include @royrok's answer.


Instead of checking the map again, the application iterates through all the playlists in MediaStore and checks the length of the _data field. I found that for all lists without an associated M3U file, this field was always empty. Then it was just a case of searching for the source code for the original Android Android application, searching for the deletion method and using it to delete any playlists with a length of 0. I renamed the PlaylistPurge application (since it doesnโ€™t โ€œre-scanโ€) and will send the code below :

 package com.roryok.PlaylistPurge; import java.util.ArrayList; import java.util.List; import android.app.ListActivity; import android.content.ContentUris; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.widget.ArrayAdapter; import android.widget.ListAdapter; public class PlaylistPurge extends ListActivity { private List<String> list = new ArrayList<String>(); private final String [] STAR= {"*"}; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ListAdapter adapter = createAdapter(); setListAdapter(adapter); } /** * Creates and returns a list adapter for the current list activity * @return */ protected ListAdapter createAdapter() { // return play-lists Uri playlist_uri= MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI; Cursor cursor= managedQuery(playlist_uri, STAR, null,null,null); cursor.moveToFirst(); for(int r= 0; r<cursor.getCount(); r++, cursor.moveToNext()){ int i = cursor.getInt(0); int l = cursor.getString(1).length(); if(l>0){ // keep any playlists with a valid data field, and let me know list.add("Keeping : " + cursor.getString(2) + " : id(" + i + ")"); }else{ // delete any play-lists with a data length of '0' Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, i); getContentResolver().delete(uri, null, null); list.add("Deleted : " + cursor.getString(2) + " : id(" + i + ")"); } } cursor.close(); // publish list of retained / deleted playlists ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list); return adapter; } } 

Here is a link to a post on my blog about the application http://roryok.com/blog/index.php/2010/07/23/clearing-out-deleted-playlists-in-android/


UPDATE

I found an article on "Requesting and Removing Media from the Android Media Store" , I have included the content below. .
.
,

Android provides a way to register various types of media, such as audio, video and images, for consumption by any application. This is convenient if your application, say, a music player or image editor. Android MediaStore is the provider of this metadata and includes information about media such as title, artist, size and location.

If your application creates some kind of media content creation, such as editing images or downloading audio from an external website, you usually want this content to be accessible from any other applications that might use it. When you create a file, you can use MediaScannerConnection to add the file and its metadata to MediaStore.

If you delete a file from the file system, the metadata remains in the MediaStore until Android scans the system for new media, which usually happens when the system first boots up or can be called explicitly called this way:

 sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory() ))); 

While this method works, it is time and resource, since it is generally necessary to scan the entire file system. An alternative is to explicitly delete the file from MediaStore. We will discuss two ways to do this. The first is a request to MediaStore for content based on some predicate and removal based on the unique identifier that MediaStore identifies. A second and simpler way to do this is to simply specify the predicate in the delete statement. In this example, I'm going to delete a sound file based on its name and file path, but you can easily use it to delete any type of media based on any known information (for example, video length or image size).

When querying MediaStore, you should think of it as an SQL database. You need to form your query by specifying a table (external MediaStore content table), the necessary columns (content identifier) โ€‹โ€‹and a where clause (how to define the content). To execute the actual query, we used the ContentResolver query () method.

 String[] retCol = { MediaStore.Audio.Media._ID }; Cursor cur = context.getContentResolver().query( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, retCol, MediaStore.MediaColumns.DATA + "='" + filePath + "'", null, null); if (cur.getCount() == 0) { return; } cur.moveToFirst(); int id = cur.getInt(cur.getColumnIndex(MediaStore.MediaColumns._ID)); cur.close(); 

The first query () argument indicates the columns that we want to return, which in this case is only "_ID". The second argument indicates that we want to look at the media stored on an external SD card (which will be the internal memory on devices without an SD card). The third argument is a predicate that determines what content we are looking for. In this case, I identify the file by its path in the file system (this is what is stored in the MediaColumns.DATA column). The fourth and fifth columns are predicate arguments and order, respectively. I include my predicative arguments in the predicate itself, so itโ€™s not necessary, and if your search for only one piece of content and your predicate is specific enough to just return a single row, then ordering does not matter.

It is very important to make the predicate specific enough so that you are guaranteed to get the exact identifier you are looking for. In my case, I know that there can only be one file in a specific place, but you can use a combination of any columns (e.g. title, artist and album) to find the content. Check out MediaColumns for all the features.

After executing the actual request, you want to check if the MediaStore really contains the content that you are trying to delete. If you fail to do this in any way, your application will crash when you try to iterate over the cursor. Once you confirm that the query returned some data, take the identifier by moving the cursor to its first position, reading the column "_ID" and closing the cursor. It is very important that you remember to close the cursor as soon as you finish using it. Your application will not crash, but you will get memory leaks and complaints in LogCat.

Now that we have the identifier that MediaStore associates with our content, we can call the ContentResolver delete () method, similar to what we called its query () method.

 Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id); context.getContentResolver().delete(uri, null, null); 

The delete () method takes 3 arguments: Uri for deletion, predicate and predicate arguments. We configure the Uri by adding the identifier that we found by querying the MediaStore in the Uri for the audio files on the external storage. Since we know exactly which row we want to delete, we do not need to specify a predicate or predicate arguments.

The second way to remove content from MediaStore uses the fact that the request and removal from it are performed almost identically.

 context.getContentResolver().delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, MediaStore.MediaColumns.DATA + "='" + path + "'", null); 

We can use the delete () method predicate to indicate exactly what we want to delete, rather than requesting it in advance. Although this method is more efficient (no additional queries, no cursors to work with), it has some pitfalls. You have no way to explicitly confirm that you are deleting. You also cannot perform advanced queries using this method, for example, if you want to remove recently added content (which you could do by ordering a query based on the DATE_ADDED column). However, in both cases, you can confirm that you deleted, since the delete () method returns the number of rows that it deleted as an integer.

+1
source share

All Articles