Set Notification Sound from Assets folder
Solution 1
put ur mp3 file in res->raw folder
, try to fetch sound file from raw folder
.
Uri path = Uri.parse("android.resource://com.androidbook.samplevideo/" + R.raw.myvideo);
OR
Uri path = Uri.parse("android.resource://com.androidbook.samplevideo/raw/myvideo");
Solution 2
This worked perfectly for me. I found it on another post.
notification.sound = Uri.parse("android.resource://my.package.name/raw/notification");
Solution 3
assets are private resources to that app, I think you wont be able to set them as ringtone, you will need a public file which is accessible by the media process to set ringtone. You will have to copy the file to sdcard to set it as ringtone.
Related videos on Youtube
Dharmendra
https://github.com/Dharmendra10 I am here for learning and help other if they come to me for help. I am little lazy so not often adding blogs on http://dharmendra4android.blogspot.in.
Updated on April 27, 2020Comments
-
Dharmendra almost 4 years
I am attaching the sound files in the assets folder for play sound in the notification but I am not able to play sound from the assets folder.
I have setting page from which user can set the sound for the notification and also user can play sound. This part is completed. I am able to play sound from the assets folder using Media Player. I can use same logic at the Notification creation time but problem can be happened when user click on the Notification because media player still playing the sound.
I tried
notification.sound = Uri.parse("file:///android_asset/Sound/Long/AlarmClock.mp3");
also tried like this
notification.sound = Uri.parse("android_asset/Sound/Long/AlarmClock.mp3");
but my issue is still not solved.
Is there any other way to set the sound in the notification from the assets directory ?
Logcat
01-10 09:57:01.509: ERROR/PlayerDriver(31): Command PLAYER_SET_DATA_SOURCE completed with an error or info PVMFErrNotSupported 01-10 09:57:01.509: ERROR/MediaPlayer(52): error (1, -4) 01-10 09:57:01.519: WARN/NotificationService(52): error loading sound for android_asset/Sound/Long/audio.mp3 01-10 09:57:01.519: WARN/NotificationService(52): java.io.IOException: Prepare failed.: status=0x1 01-10 09:57:01.519: WARN/NotificationService(52): at android.media.MediaPlayer.prepare(Native Method) 01-10 09:57:01.519: WARN/NotificationService(52): at android.media.AsyncPlayer.startSound(AsyncPlayer.java:64) 01-10 09:57:01.519: WARN/NotificationService(52): at android.media.AsyncPlayer.access$200(AsyncPlayer.java:33) 01-10 09:57:01.519: WARN/NotificationService(52): at android.media.AsyncPlayer$Thread.run(AsyncPlayer.java:99) 01-10 09:57:01.519: WARN/PlayerDriver(31): PVMFInfoErrorHandlingComplete
or any other idea ? Please give me hint.
-
Dharmendra over 12 yearsOk let me try with three slashes
-
Dharmendra over 12 yearsYeah this is great idea to create content provider to access internal files
-
Dharmendra over 12 yearsYeah I think you are right. to access the private resources I will have to use content provider.
-
Dharmendra over 11 yearsYour Uri indicates that you are accessing the resource folder rather than from assets. You are referencing a file from row folder inside your resources.
-
Dharmendra over 11 yearsIndeed this is good that we put a sound file into the row folder and refer that file.
-
leontx almost 11 yearsNote, i found that the sound had to be an .mp3 to work, not .ogg (like the rest of our sounds). And, as rajpara showed above, it also seemed necessary to not include the extension. So my sound file, push.mp3 in the myproject/res/raw folder, worked with: android.resource://com.mycompany.myapp/raw/push
-
wutzebaer over 10 yearsand how can i get the Ringtone from this AssetFileDescriptor?
-
Chiatar over 10 years@wutzebaer You can give NotificationCompat.Builder a URI of a sound to play with the notification. openAssetFile in your content provider is given this URI and returns the AssetFileDescriptor to the notification to play developer.android.com/reference/android/support/v4/app/…
-
Zia Ur Rahman over 7 years@rajpara Please tell me how to code to set this Uri as Ringtone, Notification and Alarm. I didn't find something relevant.
-
Steve Strates about 5 yearsThe first approach of referencing
R.raw.myvideo
will stop working if that resource id ever changes, since the Notification Channel never lets you change the sound/path. See this post: stackoverflow.com/a/54796493/5301868 -
greeble31 over 4 yearsCan't emphasize enough: If you are using this scheme to produce a sound URI to use with a
NotificationChannel
, be aware that the resource ID can change between builds. And, since you can't edit the URI associated with a givenNotificationChannel
(once it has been established), this will cause your app's notification sounds to break randomly after updates. Use this pattern instead; avoid using the integer resource ID. -
greeble31 over 4 yearsThe question does not concern ringtones.