Home » Android » android – Kotlin Schedule notification with mp3 resource-Exceptionshub

android – Kotlin Schedule notification with mp3 resource-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

i have a folder of mp3 files that i need to schedule a notification from , so i put them in

assests/sounds/1—-10.mp3

i can easily access them with

private fun listAssetFiles(path: String): ArrayList<String> {
    try {
        var list = getAssets().list(path)!!
        var res = ArrayList<String>()
        for( item in list ) {
            res.add("$path/$item") 
        }
        return res
    } catch (e: IOException) {
        return arrayListOf()
    }

}

for playing with MediaPlayer inside a listView , but all previous answers needs them inside res folder

Notification noti = new Notification.Builder(this) 
setSound(Uri.parse("android.resource://" + v.getContext().getPackageName() + "/" + 
R.raw.yourmp3file)) 

so how can i set path from assets or its not possible

How to&Answers:

Create raw folder in your res folder. Set your mp3 file in raw folder. Now set sound in MediaPlayer

var sound: Uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.danger_alarm_sound)

val mediaPlayer: MediaPlayer
            mediaPlayer =
                MediaPlayer.create(
                    applicationContext,
                    sound
                )
            mediaPlayer.start()