Home > Blockchain >  Android setSound - cannot get Uri of file
Android setSound - cannot get Uri of file

Time:12-09

I've seen many articles on how to perform the setSound part of this but as yet I have been unable to simply reference the file I would like played. I am using Capacitor 4 and so the file I wish to play maybe stored in a different place than a native app.

Below is the file structure of the application and I would simply like to load the "FinalCountdown.wav"

Application structure

Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE   "://"   getApplicationContext().getPackageName()   "/assets/public/assets/sounds/FinalCountdown.wav");  //Here is FILE_NAME is the name of file that you want to play
      File file = new File(sound.toString());
      Log.w("[PUSH NOTIFICATION]","File Check");
      if(file.exists()){
        Log.w("[PUSH NOTIFICATION]","FILE FOUND at "   sound.toString());
      }else{
        Log.w("[PUSH NOTIFICATION]","File NOT FOUND!!! at "   sound.toString());
      }

The check always logs (package name obfuscated)

W/[PUSH NOTIFICATION]: File Check
W/[PUSH NOTIFICATION]: File NOT FOUND!!! at android.resource://**********/assets/public/assets/sounds/FinalCountdown.wav

For completeness I have the code below where I add the sound to the notification channel

AudioAttributes audioAttributes = new AudioAttributes.Builder()
        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
        .setUsage(AudioAttributes.USAGE_NOTIFICATION)
        .build();

channel.setSound(sound, audioAttributes);

I realise this must be really simple so any help appreciated.

CodePudding user response:

Can use :

after save inputStream to internal storage and using.

AssetManager assetManager = context.getAssets();
InputStream inputStream = assetManager .open("FinalCountdown.wav");

CodePudding user response:

Put file sounds to res/raw, then use

channel.setSound(Uri.parse("android.resource://"
                          context.packageName   "/$soundRawId"), audioAttributes)
  • Related