Coder Social home page Coder Social logo

Comments (17)

optikalefx avatar optikalefx commented on June 3, 2024 1

I have an easy solution for this for anyone interested. Use CDV urls and then just resolve them to a full path.

export const getFile = function(path) {
    return new Ember.RSVP.Promise( (resolve) => {
        window.resolveLocalFileSystemURL(path, function(entry) {
            resolve(entry);
        });
    });
};

then you can

let path = fileEntry.toURL();
// need a root level path
path = path.replace('file://', '');

then use my fork which eliminates the hardcoded www, so you can play files from anywhere on your device.

https://github.com/optikalefx/cordova-plugin-lowlatencyaudio/commit/7aaf6ce948a697d344efe0e186aa017cf6569bc4

from cordova-plugin-nativeaudio.

fidoboy avatar fidoboy commented on June 3, 2024

👍 please

from cordova-plugin-nativeaudio.

gylippus avatar gylippus commented on June 3, 2024

@wootwoot1234 @fidoboy Have either of you found a solution or workaround to this?

Thanks.

from cordova-plugin-nativeaudio.

fidoboy avatar fidoboy commented on June 3, 2024

no :(

from cordova-plugin-nativeaudio.

gylippus avatar gylippus commented on June 3, 2024

@fidoboy turns out I may be onto something.

In the LowLatencyAudio.m file around line 52 is where it is looking for the file.

I have modified this based on some code I found at https://github.com/floatinghotpot/cordova-plugin-lowlatencyaudio/blob/master/src/ios/LowLatencyAudio.m and from the CDVFile.m file for finding files.

Basically I am checking for the file in two different places so that I can use local files in the www and ones that I have downloaded and am passing the full CDV File path (cordova.file.dataDirectory + ). This is for iOS only, but I presume there will be something similar for Android if it is needed there too.

NSNumber* existingReference = [audioMapping objectForKey: audioID];
        if (existingReference == nil)
        {
            NSString* basePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"www"];
            NSString* path = [NSString stringWithFormat:@"%@", assetPath];
            NSString* pathFromWWW = [NSString stringWithFormat:@"%@/%@", basePath, assetPath];

            NSURL *fileURL = [NSURL URLWithString:path];
            NSURL *resolvedFileURL = [fileURL URLByResolvingSymlinksInPath];
            path = [resolvedFileURL path];
            if ([[NSFileManager defaultManager] fileExistsAtPath : path])
            {
                NSLog(@"could find file at path: %@", path);
                LowLatencyAudioAsset* asset = [[LowLatencyAudioAsset alloc] initWithPath:path withVoices:voices];
                [audioMapping setObject:asset  forKey: audioID];

                pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: CONTENT_LOAD_REQUESTED];
                [self.commandDelegate sendPluginResult:pluginResult callbackId:callbackID];
            }
            else if ([[NSFileManager defaultManager] fileExistsAtPath : pathFromWWW]) {
                NSLog(@"could find file at pathFromWWW: %@", pathFromWWW);
                LowLatencyAudioAsset* asset = [[LowLatencyAudioAsset alloc] initWithPath:pathFromWWW withVoices:voices];
                [audioMapping setObject:asset  forKey: audioID];

                pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: CONTENT_LOAD_REQUESTED];
                [self.commandDelegate sendPluginResult:pluginResult callbackId:callbackID];
            }
            else
            {
                NSLog(@"could not file: %@", path);
                pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString: ERROR_NOT_FOUND];        
                [self.commandDelegate sendPluginResult:pluginResult callbackId:callbackID];
            }
        }
        else 
        {
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: WARN_EXISTING_REFERENCE];        
            [self.commandDelegate sendPluginResult:pluginResult callbackId:callbackID];
        }

from cordova-plugin-nativeaudio.

gylippus avatar gylippus commented on June 3, 2024

@fidoboy I just realized that I was using an even older version of this plugin so I believe it would actually be more like this for this project

            NSString* basePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"www"];
            NSString* path = [NSString stringWithFormat:@"%@", assetPath];
            NSString* pathFromWWW = [NSString stringWithFormat:@"%@/%@", basePath, assetPath];

            NSURL *fileURL = [NSURL URLWithString:path];
            NSURL *resolvedFileURL = [fileURL URLByResolvingSymlinksInPath];
            path = [resolvedFileURL path];

            if ([[NSFileManager defaultManager] fileExistsAtPath : path]) {
                NSLog(@"could find file at path: %@", path);
                LowLatencyAudioAsset* asset = [[LowLatencyAudioAsset alloc] initWithPath:path withVoices:voices withVolume:volume];
                [audioMapping setObject:asset  forKey: audioID];

                [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: CONTENT_LOAD_REQUESTED] callbackId:callbackId];
            } else if ([[NSFileManager defaultManager] fileExistsAtPath : pathFromWWW]) {
                NSLog(@"could find file at pathFromWWW: %@", pathFromWWW);
                LowLatencyAudioAsset* asset = [[LowLatencyAudioAsset alloc] initWithPath:pathFromWWW withVoices:voices withVolume:volume];
                [audioMapping setObject:asset  forKey: audioID];

                [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: CONTENT_LOAD_REQUESTED] callbackId:callbackId];
            } else {
                NSLog( @"audio file not found" );
                [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString: ERROR_NOT_FOUND] callbackId:callbackId];
            }

from cordova-plugin-nativeaudio.

fidoboy avatar fidoboy commented on June 3, 2024

Thanks gylippus your code is very useful, but it could be pretty cool to have the same on android.

from cordova-plugin-nativeaudio.

ddresch avatar ddresch commented on June 3, 2024

Hi @gylippus can you provide a working fork for this issue? I'm trying to integrate your posted code but struggling with errors like error: use of undeclared identifier 'voices' which is definitely a really simple case but I'm not Cocoa Dev so I just need to get it running :) Thx a lot if possible.

from cordova-plugin-nativeaudio.

ddresch avatar ddresch commented on June 3, 2024

@gylippus helps sometimes to ask and the answers follow by them self. Got it at least compiling now so no need to answer. Thanks!

from cordova-plugin-nativeaudio.

gylippus avatar gylippus commented on June 3, 2024

@ddresch glad to help :)

-Rubber Duck

from cordova-plugin-nativeaudio.

RShergold avatar RShergold commented on June 3, 2024

Are there any updates on this feature? I'm looking to play multiple audio tracks downloaded and saved in the app's Documents dir.

from cordova-plugin-nativeaudio.

michaelkariv avatar michaelkariv commented on June 3, 2024

Anyone implemented the fix for Android? I wish "file://" instead of appending "www/" to path was the implementation here

from cordova-plugin-nativeaudio.

pmwisdom avatar pmwisdom commented on June 3, 2024

Honestly, cordovas media plugin has a great function for both android and ios.

IOS for example:

// Maps a url for a resource path for playing
// "Naked" resource paths are assumed to be from the www folder as its base
- (NSURL*)urlForPlaying:(NSString*)resourcePath
{
    NSURL* resourceURL = nil;
    NSString* filePath = nil;

    // first try to find HTTP:// or Documents:// resources

    if ([resourcePath hasPrefix:HTTP_SCHEME_PREFIX] || [resourcePath hasPrefix:HTTPS_SCHEME_PREFIX]) {
        // if it is a http url, use it
        NSLog(@"Will use resource '%@' from the Internet.", resourcePath);
        resourceURL = [NSURL URLWithString:resourcePath];
    } else if ([resourcePath hasPrefix:DOCUMENTS_SCHEME_PREFIX]) {
        NSString* docsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
        filePath = [resourcePath stringByReplacingOccurrencesOfString:DOCUMENTS_SCHEME_PREFIX withString:[NSString stringWithFormat:@"%@/", docsPath]];
        NSLog(@"Will use resource '%@' from the documents folder with path = %@", resourcePath, filePath);
    } else if ([resourcePath hasPrefix:CDVFILE_PREFIX]) {
        CDVFile *filePlugin = [self.commandDelegate getCommandInstance:@"File"];
        CDVFilesystemURL *url = [CDVFilesystemURL fileSystemURLWithString:resourcePath];
        filePath = [filePlugin filesystemPathForURL:url];
        if (filePath == nil) {
            resourceURL = [NSURL URLWithString:resourcePath];
        }
    } else {
        // attempt to find file path in www directory or LocalFileSystem.TEMPORARY directory
        filePath = [self.commandDelegate pathForResource:resourcePath];
        if (filePath == nil) {
            // see if this exists in the documents/temp directory from a previous recording
            NSString* testPath = [NSString stringWithFormat:@"%@/%@", [NSTemporaryDirectory()stringByStandardizingPath], resourcePath];
            if ([[NSFileManager defaultManager] fileExistsAtPath:testPath]) {
                // inefficient as existence will be checked again below but only way to determine if file exists from previous recording
                filePath = testPath;
                NSLog(@"Will attempt to use file resource from LocalFileSystem.TEMPORARY directory");
            } else {
                // attempt to use path provided
                filePath = resourcePath;
                NSLog(@"Will attempt to use file resource '%@'", filePath);
            }
        } else {
            NSLog(@"Found resource '%@' in the web folder.", filePath);
        }
    }
    // if the resourcePath resolved to a file path, check that file exists
    if (filePath != nil) {
        // create resourceURL
        resourceURL = [NSURL fileURLWithPath:filePath];
        // try to access file
        NSFileManager* fMgr = [NSFileManager defaultManager];
        if (![fMgr fileExistsAtPath:filePath]) {
            resourceURL = nil;
            NSLog(@"Unknown resource '%@'", resourcePath);
        }
    }

    return resourceURL;
}

from cordova-plugin-nativeaudio.

michaelkariv avatar michaelkariv commented on June 3, 2024

I am using cordovaMedia (via its angular wrapper ngCordova) and the problem is the delay. May be it is because there is no preload, like in NativeAudio.

from cordova-plugin-nativeaudio.

sommerper avatar sommerper commented on June 3, 2024

How has this not been resolved yet? It's pretty important not being restricted to the www/ folder.

from cordova-plugin-nativeaudio.

optikalefx avatar optikalefx commented on June 3, 2024

My fork has this resolved.

from cordova-plugin-nativeaudio.

RoryPicko avatar RoryPicko commented on June 3, 2024

simply...

path = path.replace('file://', '');

-- the above worked for ios, android needed a little more

in the folder plugin/cordova-plugin-nativeaudio/src/android

edit line 85 of NativeAudio.java from
String fullPath = concat("www"+assetPath);
to
String fullPath = assetPath;

you will then need to remove the android platform and then re-add it for the plugin to update

cordova platforms rm android
cordova platforms add android

from cordova-plugin-nativeaudio.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.