Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assetPath and audio file placement #41

Open
HunSpeedi opened this issue Sep 23, 2021 · 21 comments
Open

assetPath and audio file placement #41

HunSpeedi opened this issue Sep 23, 2021 · 21 comments

Comments

@HunSpeedi
Copy link

Description of the bug:
In the Readme.md the following is stated:

All audio place in specific platform folder
Andoid: android/app/src/assets
iOS: ios/App/App/sounds

This seems to be incorrect!

Suggestion for a fix:
All audio should be placed inside the specific folders:
Android: android/app/src/main/assets
iOS: ios/App/App/public/assets/

Note: in the case of Android the assethPath is just the filename with the extension like 'sound.mp3' but in the case of iOS
you need to specify more details like: assetPath: 'public/assets/sound.mp3'

@HunSpeedi HunSpeedi changed the title assetPath and audio file placement description assetPath and audio file placement Sep 23, 2021
@lordrepha1980
Copy link

It dont works for me. I Placed the Audiofiles in the src-capacitor/ios/App/App/public/assets/ Folder

And my methode call is:

NativeAudio.preload({
                assetPath:'public/assets/NeueNachrichtWav.wav',
                assetId: 'neuefahrt',
                volume: 1.0,
                isUrl: false
            });

Error Message from XCode

[error] - {"errorMessage":"Asset Path is missing - public/assets/NeueNachrichtWav.wav","message":"Asset Path is missing - public/assets/NeueNachrichtWav.wav"}

Sorry i need help.

@nprail
Copy link

nprail commented Oct 19, 2021

I am experiencing this issue as well.

@lordrepha1980
Copy link

lordrepha1980 commented Oct 19, 2021

Ok great I am not alone. Where do you placed, your audio files?

@nprail
Copy link

nprail commented Oct 21, 2021

It worked when I placed the sound file in the src/assets folder (assuming you are using Angular) so that it is copied to each platform's respective public folders when you run npx cap copy.

Then I was able to use the file like so:

NativeAudio.preload({
    assetPath: 'public/assets/test.mp4',
    assetId: 'test',
    isUrl: false
})

So far I have only tested this on iOS. I assume @HunSpeedi's note about a different path on Android is correct as well.

@HunSpeedi
Copy link
Author

@lordrepha1980 If you have followed my instructions under "Suggestion for a fix" it should work.
Also if you want it to work on both iOS and Android you need to create an else if :/
Something like this:

if (this.platform.is('android')) {
      NativeAudio.preload({
        assetPath: 'sound.mp3',
        assetId: 'my_sound',
        volume: 1.0,
        audioChannelNum: 1,
      });
    } else if (this.platform.is('ios')) {
      NativeAudio.preload({
        assetPath: 'public/assets/sound.mp3',
        assetId: 'my_sound',
        volume: 1.0,
        audioChannelNum: 1,
      });
    }

@lordrepha1980
Copy link

@lordrepha1980 If you have followed my instructions under "Suggestion for a fix" it should work. Also if you want it to work on both iOS and Android you need to create an else if :/ Something like this:

if (this.platform.is('android')) {
      NativeAudio.preload({
        assetPath: 'sound.mp3',
        assetId: 'my_sound',
        volume: 1.0,
        audioChannelNum: 1,
      });
    } else if (this.platform.is('ios')) {
      NativeAudio.preload({
        assetPath: 'public/assets/sound.mp3',
        assetId: 'my_sound',
        volume: 1.0,
        audioChannelNum: 1,
      });
    }

Sorry, it doesn't work.

@HunSpeedi
Copy link
Author

@lordrepha1980 And you have checked in your code editor if the files are in the given location within the ios and android folders?
Also, made sure that in the device all the sound settings are turned on?

@lordrepha1980
Copy link

Sorry, it doesnt work i use Quasar. I have my files into root/src/assets/audio. All Background Audio Services are on.

@nprail
Copy link

nprail commented Oct 27, 2021

For me, it seems to only work using the same path on both Android and iOS. Not sure why mine is behaving different than @HunSpeedi.

I did notice that if I place the audio file in a sub-folder of src/assets, Capacitor will flatten the assets folder when copying to each platform.

So for example, if I have a file, src/assets/sounds/sound.mp3 and then run npx cap copy, it will copy the file to these locations instead:

  • Android: android/app/src/main/assets/public/assets/sound.mp3
  • iOS: ios/App/App/public/assets/swiftly.mp3

And then I can use it like so:

NativeAudio.preload({
  assetId: 'sound',
  assetPath: 'public/assets/sound.mp3',
  audioChannelNum: 1,
  isUrl: false,
});

@Merwan1010
Copy link

Merwan1010 commented Nov 5, 2021

Something that worked for me (for ios) :

  • Open Xcode
  • Go to "Build phases"
  • On "Copy Bundle ressources" click on the add button (+)
  • Click on the button "Add other" and select your .mp3 file (it can be wherever you want inside the ios project)
  • Make sure to check "Copy Items if needed", then press "Finish"

Xcode seems to copy the file to the root of the project, you can now load and play the file using the following code :

NativeAudio.preload({
  assetId: "fire",
  assetPath: "fire.mp3", 
  audioChannelNum: 1,
  isUrl: false
});

Screen Shot 2021-11-05 at 16 19 52

Tell me if it worked for you !

@HunSpeedi
Copy link
Author

For me, it seems to only work using the same path on both Android and iOS. Not sure why mine is behaving different than @HunSpeedi.

I did notice that if I place the audio file in a sub-folder of src/assets, Capacitor will flatten the assets folder when copying to each platform.

So for example, if I have a file, src/assets/sounds/sound.mp3 and then run npx cap copy, it will copy the file to these locations instead:

  • Android: android/app/src/main/assets/public/assets/sound.mp3
  • iOS: ios/App/App/public/assets/swiftly.mp3

And then I can use it like so:

NativeAudio.preload({
  assetId: 'sound',
  assetPath: 'public/assets/sound.mp3',
  audioChannelNum: 1,
  isUrl: false,
});

I have checked, and my implementation works the same :)

@HunSpeedi
Copy link
Author

Sorry, it doesnt work i use Quasar. I have my files into root/src/assets/audio. All Background Audio Services are on.

You need to check if your files are within the ios and android folders!
Check out @nprail-s comment, if you repeat the steps that He described it should work!
Ps.: Make sure that you check out your root/ios/App/App/sounds and root/android/app/src/assets folders if they contain the desired folder structure and files (if not, just copy them there?!)

@Merwan1010
Copy link

For me, it seems to only work using the same path on both Android and iOS. Not sure why mine is behaving different than @HunSpeedi.

I did notice that if I place the audio file in a sub-folder of src/assets, Capacitor will flatten the assets folder when copying to each platform.

So for example, if I have a file, src/assets/sounds/sound.mp3 and then run npx cap copy, it will copy the file to these locations instead:

  • Android: android/app/src/main/assets/public/assets/sound.mp3
  • iOS: ios/App/App/public/assets/swiftly.mp3

And then I can use it like so:

NativeAudio.preload({
  assetId: 'sound',
  assetPath: 'public/assets/sound.mp3',
  audioChannelNum: 1,
  isUrl: false,
});

In my case Capacitor didn't flatten the directories.
One thing which is important if you are using react is to make a build (react-scripts build) and then do npx cap copy.

After doing so the audio files is accessible from the public directory in both android and ios.

Screen Shot 2021-11-12 at 11 42 51

Screen Shot 2021-11-12 at 11 42 21

@mariusbolik
Copy link

If you want to download a mp3 file from a server, store it locally and play it, please see my code example here: #39 (comment)
Maybe it will help 🙂

@arielhasidim
Copy link

Same for me in android. Couldn't preload when asset was in android/app/src/assets, but works in android/app/src/main/assets.

@riderx
Copy link
Contributor

riderx commented Feb 16, 2022

Worked for me with public/ in path, don't need to add your self in android studio or xcode

@lordrepha1980
Copy link

this worked fpr me #41 (comment) thank you guys

@kyoz
Copy link
Contributor

kyoz commented Mar 3, 2022

@nprail's solution work for me, just one different is it not flatten my sounds folder. (I'm on ionic 6 and capacitor 3.4.1)

Assume you place your sounds in src/assets/sounds/bang.wav

Then run:

ng build
npx cap copy

Ensure you won't forgot ng build command, or else it will copy the old assets folder and there is no sounds folder you've just added.

Then you can load it with something like:

NativeAudio.preload({
  assetId: 'bang',
  assetPath: `public/assets/sounds/bang.wav`,
  audioChannelNum: 1,
  isUrl: false
});

Hope it help 🐱

@HarelM
Copy link

HarelM commented Aug 7, 2022

I'm using Android, iOS and web with hopefully the same code.
I place my mp3 file under src/contect/music.mp3 of my angular project which get copied to both platforms.
I don't want to place it in a different place since I would like to use it in my web page and not duplicate it (DRY principle).
Since Android works for me only with public/content/music.mp3 I need to set different folders for Android and web (I haven't got to try iOS yet).
I would expected the native plugin to try and use my files in the public folder if I give it relate path and not absolute path and by that allowing to use the same code for all platforms.
i.e I expect the following to work in all platforms (including web):

NativeAudio.preload({
  assetId: 'music',
  assetPath: `content/music.mp3`, // relative path to base URL on server and public folder on native
  isUrl: false
});

As far as I understand, this is currently not the case.
There's also some "if" related to content folder which I didn't understand...


This seems like a special way to get a resource, but I would expect it to be something like content: so that it doesn't mix with the relative folder hierarchy.

Let me know if there's anything I can do to help solve this (e.g. PR etc)
I think the general assumption in this thread is that the plugin should be able to better find the files, without the internal knowledge (i.e. public folder) of how capacitor works (IMHO at least).

@vicatcu
Copy link

vicatcu commented Feb 25, 2023

In Android how I figured out what the path should be is by finding the audio file in the assets folder in the project file tree in Android Studio, right clicking on it, choosing "Copy Path / Reference", then choosing "Path from Source Root". Put that in my code, and presto I hear a sound played.

@riderx
Copy link
Contributor

riderx commented Feb 28, 2023

i have publish npm i @capgo/native-audio who takes the path of file in your build folder.
https://github.com/Cap-go/native-audio

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants