'How can use MediaMetadataRetriever().setDataSource() while MediaStore.Audio.Media.DATA is deprecated to get the path
I'm trying to get all audio files of an android device in addition to cover image of each one of them. MediaMetadataRetriever().setDataSource() should take a path but MediaStore.Audio.Media.DATA that provides path is deprecated. I tried this but I take IllegalArgumentException. I'm not sure if I'm doing it in the right way or not by the way. This is the code:
import android.content.ContentUris
import android.content.Context
import android.database.Cursor
import android.provider.MediaStore
fun getAllAudioFromDevice(context: Context): List<AudioModel> {
val tempAudioList: MutableList<AudioModel> = ArrayList()
val projection =
arrayOf(MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.ARTIST)
val c: Cursor? =
context.contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, null, null, null)
if (c != null) {
while (c.moveToNext()) {
val mmr = android.media.MediaMetadataRetriever()
val contentUri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, c.getLong(0))
mmr.setDataSource(contentUri.path)
val coverImage = mmr.embeddedPicture
val audioModel = AudioModel(contentUri, c.getString(1), c.getString(2), c.getString(3), coverImage)
tempAudioList.add(audioModel)
}
c.close()
}
return tempAudioList
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
