Set Notification Sound from Assets folder

一曲冷凌霜 提交于 2019-12-03 09:43:28
rajpara

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");
Comacoco

This worked perfectly for me. I found it on another post.

notification.sound = Uri.parse("android.resource://my.package.name/raw/notification");

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.

The correct path to load something from your assets folder is file:///android_assets/relative_path_to_your_file. Note the three slashes here. Although I have not tried to set a notification sound this way, I have used this with a WebView with success a number of times, and would expect it to work the same way.

I was not able to access directly the assets audio file to the Notification. So i follow this steps and so I am able to play audio in the Notification

  1. I copy that files to the data directory.
  2. Still data directory is not accessible to the Notification sound so I create a custom Content Provider to share the audio file in the read more which are now in data folder.
  3. Now at the time of notification creation I had set the Uri of the file to the Notification sound.

Now using this steps I am able to set custom sound to the Notification.

ContentProvider is the way to go. I don't think there is a need to copy files to the data directory like @Dharmendra suggests. You can access the asset folder directly though the ContentProvider

public AssetFileDescriptor openAssetFile(Uri uri, String mode)
        throws FileNotFoundException {
    String filePart = Sound.getSoundFileName(uri);

    try {
        return getContext().getResources().getAssets().openFd(filePart + ".ogg");
    }catch(IOException e) {
        Log.d(TAG, "fail " + e.getMessage());

    }
    return null;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!