cordova-plugin-nativeaudio icon indicating copy to clipboard operation
cordova-plugin-nativeaudio copied to clipboard

Use cordova-plugin-nativeaudio with ionic Capacitor

Open raphrmx opened this issue 5 years ago • 7 comments

Is it to possible to update plugin for remove hardcoded www in NativeAudio.java

...
int voices;
if (data.length() <= 3) {
  voices = 1;
} else {
  voices = data.getInt(3);
}

String fullPath = "www/".concat(assetPath);

Context ctx = cordova.getActivity().getApplicationContext();
AssetManager am = ctx.getResources().getAssets();
AssetFileDescriptor afd = am.openFd(fullPath);
...

thanks in advance, Raph.

raphrmx avatar Jun 26 '19 09:06 raphrmx

@raphrmx If you replace www with public it'll all work. I'll submit a pull request to add Capacitor support when I have time. It won't be soon.

sta55en avatar Oct 23 '19 21:10 sta55en

It is the same for iOS

to avoid

[log] - (NATIVE AUDIO) Asset not found.

you need to change ios/capacitor-cordova-ios-plugins/sources/CordovaPluginNativeaudio/NativeAudio.m from: line 87: NSString* basePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"www"]; to: line 87: NSString* basePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"public"];

Reason: capacitor does not place the js files and assets folder under www but public.

wzr1337 avatar Apr 05 '20 19:04 wzr1337

Any update about this issue?

nicobytes avatar May 07 '20 11:05 nicobytes

https://github.com/codextde/cordova-plugin-nativeaudio

danielehrhardt avatar Oct 24 '20 23:10 danielehrhardt

In Ionic Capacitor if you are using preloadComplex: await this.nativeAudio.preloadComplex(unique_id, url, volume, 1, 0); then change NativeAudio.m line 172 to:

NSString*` basePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"public"];
NSString* path = [NSString stringWithFormat:@"%@/%@", basePath, assetPath];
NSString* simplePath = [NSString stringWithFormat:@"%@", assetPath];
            if ([[NSFileManager defaultManager] fileExistsAtPath : simplePath]) {
                NativeAudioAsset* asset = [[NativeAudioAsset alloc] initWithPath:simplePath
                                                                      withVoices:voices
                                                                      withVolume:volume
                                                                   withFadeDelay:delay];

                audioMapping[audioID] = asset;

                NSString *RESULT = [NSString stringWithFormat:@"%@ (%@)", INFO_ASSET_LOADED, audioID];
                [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: RESULT] callbackId:callbackId];

            } else if ([[NSFileManager defaultManager] fileExistsAtPath : path]) {
                NativeAudioAsset* asset = [[NativeAudioAsset alloc] initWithPath:path
                                                                      withVoices:voices
                                                                      withVolume:volume
                                                                   withFadeDelay:delay];

                audioMapping[audioID] = asset;

                NSString *RESULT = [NSString stringWithFormat:@"%@ (%@)", INFO_ASSET_LOADED, audioID];
                [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: RESULT] callbackId:callbackId];

            } else {
                NSString *RESULT = [NSString stringWithFormat:@"%@ (%@)", ERROR_ASSETPATH_INCORRECT, assetPath];
                [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString: RESULT] callbackId:callbackId];
            }

jorgemedhaku avatar Oct 27 '20 03:10 jorgemedhaku

Still waiting on a fix. Having to modify the plugin seems really janky, since wouldn't it be overriden on every npm install + ionic cap sync?

mikelhamer avatar Apr 05 '21 16:04 mikelhamer

@mikelhamer Yep I'm having to do the change pretty much every iOS production build and it's easy to forget... And it's a necessary feature on my project.

bchehraz avatar Oct 28 '21 16:10 bchehraz