floatinghotpot/cordova-plugin-nativeaudio

Use cordova-plugin-nativeaudio with ionic Capacitor

Opened this issue · 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 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.

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.

Any update about this issue?

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];
            }

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 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.