List all music files from media store. Is it possible?

salanmar

Member
Licensed User
Longtime User
I want to get a list of all music files that are in the tablet with the tag and the path, and save it in a database created by me.
I found this code, Can this be done with the reflection library?

B4X:
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

String[] projection = {
        MediaStore.Audio.Media._ID,
        MediaStore.Audio.Media.ARTIST,
        MediaStore.Audio.Media.TITLE,
        MediaStore.Audio.Media.DATA,
        MediaStore.Audio.Media.DISPLAY_NAME,
        MediaStore.Audio.Media.DURATION
};

cursor = this.managedQuery(
        MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
        projection,
        selection,
        null,
        null);

private List<String> songs = new ArrayList<String>();
while(cursor.moveToNext()){
        songs.add(cursor.getString(0) + "||" + cursor.getString(1) + "||" +   cursor.getString(2) + "||" +   cursor.getString(3) + "||" +  cursor.getString(4) + "||" +  cursor.getString(5));
}
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It will be easier to write a small library for this.

Note that you can use ContentChooser (Phone library) to let the user choose a song:
B4X:
Sub Process_Globals
   Dim cc As ContentChooser

End Sub
Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      cc.Initialize("CC")
   End If
   cc.Show("audio/*", "Choose audio file")
End Sub
Sub CC_Result (Success As Boolean, Dir As String, FileName As String)
   
End Sub
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
I've been asking for a library for this.

Also, the content chooser is completely different.
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
Could you please add the following code to it so we can control the media player?

long eventtime = SystemClock.uptimeMillis();
int code = KeyEvent.KEYCODE_MEDIA_NEXT; ' let us change the keycode as a parameter (next, prev, or play/pause)

Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);

KeyEvent downEvent = new KeyEvent(eventtime, eventtime,

KeyEvent.ACTION_DOWN, code, 0);

downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent);

context.sendOrderedBroadcast(downIntent, null);

Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null);

KeyEvent upEvent = new KeyEvent(eventtime, eventtime,

KeyEvent.ACTION_UP, code, 0);

upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent);

context.sendOrderedBroadcast(upIntent, null);
 
Upvote 0
Top