BRUTAL
BRUTAL

Reputation: 1

Album Id and Duration is 0 in Content Provider

I am trying to get audio files from user device content. All parameters such as name, artist or size match the found audio. But the album id and duration are equal to 0. Moreover, if you somehow move the audio file in the system or change the file name, the album id and duration are no longer 0, but correspond to the audio file.

Here is my code in Kotlin:

 override fun getMusic(): Single<List<Music>> {
        return Single.create { subscriber ->
            try {
                val listMusic = ArrayList<Music>()
                val contentResolver = context.contentResolver
                val projection = arrayOf(
                    MediaStore.Audio.AudioColumns.DATA,
                    MediaStore.Audio.AudioColumns.ALBUM_ID,
                    MediaStore.Audio.ArtistColumns.ARTIST,
                    MediaStore.Audio.AudioColumns.TITLE,
                    MediaStore.Audio.Media.DURATION,
                    MediaStore.Audio.Media.SIZE,
                    MediaStore.Audio.Media._ID,
                )

                val audioCursor =
                    contentResolver.query(uri, projection, null, null, null).use { cursor ->
                        cursor?.let {
                            if (cursor.count > 0) {
                                while (cursor.moveToNext()) {
                                    val data = cursor.getString(0)
                                    //album id is 0 if the audio file was not moved
                                    val albumId = cursor.getLong(1)
                                    val artist = cursor.getString(2)
                                    val name = cursor.getString(3)
                                    //duration is 0 if the audio file was not moved
                                    val duration = cursor.getLong(4)
                                    val size = cursor.getString(5)
                                    val musicId = cursor.getLong(6)
                                    val artUri = getAlbumArt(albumId).toString()
                                    val musicUri = getMusicUriById(musicId).toString()
                                    val music = Music(
                                        artUri = artUri,
                                        uri = musicUri,
                                        data = data,
                                        artist = artist,
                                        size = size,
                                        name = name,
                                        duration = duration
                                    )
                                    listMusic.add(parseMusic(music))
                                }
                                subscriber.onSuccess(listMusic)
                            }
                        }
                    }

            } catch (e: IllegalStateException) {
                subscriber.onError(e)
            }
        }
    }

Upvotes: 0

Views: 68

Answers (0)

Related Questions