Hey guys, I'm working on a ringtone picker as part...
# android
a
Hey guys, I'm working on a ringtone picker as part of my app, part of that is listing all audio files on the user device and playing them individually upon user request, thread 🧵
stackoverflow 2
not kotlin but kotlin colored 3
So I use the MediaStore API to list all the audio files as follows:
Copy code
val projection = arrayOf(
    MediaStore.Audio.Media.DISPLAY_NAME,
    MediaStore.Audio.Media.DATA
)
val selection = MediaStore.Audio.Media.IS_MUSIC + " != 0"
appCtx.contentResolver.query(
    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
    projection,
    selection,
    null,
    null
)?.asSequence()?.map {
    Ringtone(
        name = it.getString(0),
        source = MediaSource.Path(it.getString(1))
    )
}?.toList().orEmpty()
and to play the selected file, I use android.media.MediaPlayer and give it the file path as a data source:
Copy code
player.setDataSource(appCtx, Uri.parse(source.path))
I'm requesting these permissions to do so:
Copy code
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="32" />
    <uses-permission android:name="android.permission.READ_MEDIA_AUDIO" android:minSdkVersion="33" />
I also request them at runtime, but on Android 10 devices (and probably on newer ones as well) I'm getting this error:
Copy code
java.io.FileNotFoundException: /storage/emulated/0/Samsung/Music/Over_the_Horizon.mp3: open failed: EACCES (Permission denied)
I know this's related to scoped storage, but I can't figure out from the docs how to fix it
c
Wrong workspace. Over here we talk about the Kotlin programming language and all related topics. You question is pure android and should be asked on a different platform.