capacitor-community / native-audio

MIT License
109 stars 66 forks source link

Cannot get assetPath to work #9

Closed savgiannis closed 3 years ago

savgiannis commented 4 years ago

I have this code here:

NativeAudio.preloadSimple({ assetPath: "@/assets/notif.mp3", assetId: "notif_sound" });

And I get this error from android studio:

Sending plugin error: {"save":false,"callbackId":"123755789","pluginId":"NativeAudio","methodName":"preloadSimple","success":false,"error":{"message":"Resource ID #0x0"}}

Maybe you mean the native project's assets and not the web assets, but there is no example in the docs. Where should i place my file in the native projects(ios & android) and what is the path to access it?

kheftel commented 4 years ago

on iOS, it looks for the path in the bundle. The web files are in the bundle, so they can be loaded from there. I had luck with public/assets/file.mp3, where my web files are in public.

on Android, the latest release looks for it as a raw resource, which I believe means it should go in res/raw.

In my project, I had to load files that were separately downloaded, so I made a PR (#10) to allow passing a file URL and telling the plugin it's a file url by passing isUrl.

WhippyM commented 4 years ago

I have the same problem as described above. What is the correct assetPath? The Files are located in an Angular-Project under "assets/sounds/media.mp3". Some examples for assetPaths which do not work:

What I'm doing wrong? Is there maybe a sample-project where the Plugin is working?

giacomodeliberali commented 4 years ago

We, too, are struggling to make assetPath work. We finally got it playing (on Android) by moving the audio assets under res/raw (creating a new folder raw) and by referencing a file only by its name (without the extension).

kheftel commented 4 years ago

yeah, putting the file in res/raw and leaving the extension off is how I'm getting it to play too (I'm using preloadComplex but on Android they do the same thing if you look at the native code).

I noticed the LocalNotifications built-in plugin uses a capacitor util function AssetUtil.getResourceBaseName that strips off the file extension if it's passed in. As a result, you can pass a filename + extension to that plugin and it works. I'd be willing to make a PR fix some things, but the last one I made (#10 ) which solves the problem another way (allowing you to pass a whole file:// URL) has sat for almost a month and I'd rather not make too many PRs at once.

kheftel commented 4 years ago

If you look at the android source code, you can see that it is looking for a raw resource. And apparently, looking at the android documentation, it wants a "resource name" and not a filename - perhaps that's why the extension has to be dropped.

johnson512n28 commented 3 years ago

Having separate functional requirements for each platform defeats the purpose of having a cross-platform plugin.

bazuka5801 commented 3 years ago

Fixed in new release 🌟