0

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

sheko
  • 516
  • 4
  • 15
  • pls check https://stackoverflow.com/a/13784557/7498057 – Komal Feb 21 '20 at 17:31
  • @Komal hi thanks i checked it , but for this i need the files to be inside raw folder and assets folder which is a duplicate can it refer to assets ? – sheko Feb 21 '20 at 17:40
  • pls remove file from the asset and Just put your mp3 file to raw folder in res. – Komal Feb 21 '20 at 17:43
  • @Komal ok i will , but check this https://stackoverflow.com/a/7499632/8669531 i need then to load mp3 from raw to play which needs a permission ? also will it make a difference regrading files shown in user device where he can take them ? – sheko Feb 21 '20 at 17:47
  • you just want to set custom sound to your notification and above link is for media player. – Komal Feb 21 '20 at 17:50
  • but i can't create subfolders inside raw like assets how i should handle this ? i have a lot of folders and i think it will be missy to throw them in 1 folder – sheko Feb 21 '20 at 18:05

1 Answers1

0

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()
Komal
  • 328
  • 3
  • 15