1

In my Flutter app, I've some custom sound files placed in android/app/src/main/res/raw to be used as notification tones. Now I want to play these sounds when user selects using Just audio package. Not sure how to reference and play these files (or any resources from platform folders, for that matter). For now I had to copy them to assets folder and play from there which is not a good solution obviously.

Muhammad Qasim
  • 351
  • 1
  • 6
  • 21

1 Answers1

0

For a raw resource, you can use:

AudioSource.uri(Uri('rawresource:///resourceId'))
Ryan Heise
  • 2,273
  • 5
  • 14
  • How to use this to play a file located at `res/raw/abc.m4a`? There's no such Uri constructor as in answer. Tried with `Uri(scheme: 'rawresource', path: 'raw/abc')` and `Uri(scheme: 'rawresource', path: 'abc')` but got errors. – Muhammad Qasim Dec 16 '21 at 04:33
  • I wonder if this is even possible in Flutter as mentioned in [this SO answer](https://stackoverflow.com/a/56351291/1513694) _"However, you won't be able to share a drawable item directly to flutter"_ – Muhammad Qasim Dec 16 '21 at 04:38
  • The resource name is `abc`, so you would use this: `Uri.parse('rawresource:///abc)`. The reason this should work is that just_audio passes on your URI directly to ExoPlayer which interprets it, and that is how ExoPlayer should interpret this URI. One exception is if your URI has the `asset` scheme. This will not be passed through to ExoPlayer, it will be intercepted by just_audio and will load the file from your flutter assets instead. – Ryan Heise Dec 17 '21 at 13:04
  • That gives following error: `E/ExoPlayerImplInternal(29564): Caused by: com.google.android.exoplayer2.upstream.RawResourceDataSource$RawResourceDataSourceException: Resource identifier must be an integer.` – Muhammad Qasim Dec 23 '21 at 12:32
  • For further information, you can see the ExoPlayer documentation here: https://exoplayer.dev/doc/reference/com/google/android/exoplayer2/upstream/DefaultDataSource.html just_audio will pass your URI directly to ExoPlayer and then that page explains how ExoPlayer interprets your resource URI. – Ryan Heise Dec 24 '21 at 13:30