0

I have a requirement to fetch the required audio file available in the raw folders. I am using sound pool in my application to play the audio.

So I need to pass the exact file name to the getAssets().openFd(string) to get the resource id.

What is the exact file path of music1.mp3 for instance?

Krylez
  • 17,414
  • 4
  • 32
  • 41
  • SoundPool can load using just the resource identifier (no path necessary). Do you need the path for something else? – Krylez Feb 15 '17 at 18:31
  • Yes you are right. But my case is dynamic I can't pass a static value in the code. Based on user input i need to pass the required identifier. So I need to get this dynamically how? And one more thing is these audio files can be added additionally in future. – Hari Hara Krishnan Feb 15 '17 at 18:36
  • Why dynamically fetch a static resource? Raw resources are determined at build time and nothing can be added/removed later. Could you simply create and map user-friendly label strings to the raw resource files? – Krylez Feb 15 '17 at 18:54
  • in fact I am new to android. my requirement is, I have designed some API kind of stuff where the developer can use this API and they can add some short audio files in their application with some names to them. In addition they will define the audio file as 1, 2, 3 etc and map it in a json file. my API needs to pickup the files as defined in the json using the id mapping. so ultimately if the id in json is 1 it will play the audio named as 1 in res folder or assets folder. In this case what should I use? res/raw folder or assets folder? – Hari Hara Krishnan Feb 17 '17 at 00:04
  • Since not all files are available at install time, you'll use neither raw resources nor assets. You'll want to use the third option in @CommonsWare's answer. Store and retrieve the files in a location of your choice under Context.getFilesDir(). – Krylez Feb 17 '17 at 04:51

1 Answers1

1

You are conflating three separate things:

  • Assets
  • Raw resources
  • Files on the filesystem

None of these are the same thing. In particular, neither assets nor raw resources are files on the filesystem on an Android device.

To access a file that you place in assets/ of your project, use getAssets().open() (or openFd()), with the relative path within assets/ as a parameter.

To access a raw resource, use getResources().openRawResource() (or openRawResourceFd()), with the resource ID of the resource (e.g., R.raw.music1).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for sharing clear idea of diff b/w assets and resources. In my application I have the files in raw folder. And I am trying to access only by name which will be fetched dynamically based on the name to get the resource id. Since sound pool requires resource id. Can you please clarify this. – Hari Hara Krishnan Feb 15 '17 at 18:30