How To Get All .mp3 Files From Internal And External Storage In Android
i want to make a music player. but i couldn't get all the .mp3 files from internal and external storage. can anyone help me please. Thanks in advance. here is my code. public v
Solution 1:
Please find the function below which is able to get the all mp3 songs which you want.
public void getMp3Songs() {
Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
cursor = managedQuery(allsongsuri, STAR, selection, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
song_name = cursor
.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
int song_id = cursor.getInt(cursor
.getColumnIndex(MediaStore.Audio.Media._ID));
String fullpath = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DATA));
fullsongpath.add(fullpath);
album_name = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.ALBUM));
int album_id = cursor.getInt(cursor
.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
artist_name = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.ARTIST));
int artist_id = cursor.getInt(cursor
.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));
} while (cursor.moveToNext());
}
cursor.close();
db.closeDatabase();
}
}
Solution 2:
fun getAllAudioFromDevice(context: Context): List<AudioModel>? {
val tempAudioList: MutableList<AudioModel> = ArrayList()
val uri: Uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
val projection = arrayOf<String>(
MediaStore.Audio.AudioColumns.DATA,
MediaStore.Audio.AudioColumns.TITLE,
MediaStore.Audio.ArtistColumns.ARTIST,
)
val selection = MediaStore.Audio.Media.IS_MUSIC + " != 0"
/*val projection = arrayOf(
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
)*/
val c: Cursor? = context.contentResolver.query(
uri, projection, selection, null, null
)
if (c != null) {
while (c.moveToNext()) {
val audioModel = AudioModel()
val path = c.getString(0)
val name = c.getString(1)
val artist = c.getString(2)
audioModel.trackName = name
audioModel.trackDetail = artist
audioModel.trackUrl = path
if(path != null && (path.endsWith(".aac")
|| path.endsWith(".mp3")
|| path.endsWith(".wav")
|| path.endsWith(".ogg")
|| path.endsWith(".ac3")
|| path.endsWith(".m4a"))) {
Log.v(mTAG, "trackName :${audioModel.trackName}, trackDetail :${audioModel.trackDetail}, trackUrl :${audioModel.trackUrl}")
tempAudioList.add(audioModel)
}
}
c.close()
}
return tempAudioList
}
Post a Comment for "How To Get All .mp3 Files From Internal And External Storage In Android"