Playing a media file under www

What platform are you building for?

iOS

What framework are you building with?

Cordova

What is your build ID?

5f0df44c-7952-4c47-90e5-4a5521ec99d8

What is your error message?

Audio Error: Cannot use audio file from resource ‘sonar_short.mp3’, code: 1

Please describe the issue:

I have cordova-plugin-media and cordova-plugin-file installed, and I’m trying to play a media file that’s under www in the project, and I’ve referenced documentation for these plugins as well as asking AI regarding this.

It works fine for Android using “file:///android_asset/www/sonar_short.mp3”

For iOS supposedly both “sonar_short.mp3” and “cdvfile://localhost/www/sonar_short.mp3” should work, but neither does as per the error message:
Audio Error: Cannot use audio file from resource ‘sonar_short.mp3’, code: 1
(MediaError.MEDIA_ERR_ABORTED whatever that means)

The file has been created by Audacity and plays fine in Windows and Android.

I’ve checked the IPA and the file is stored under “\Payload\CliqTags Nearby.app\www”

Update: cordova.file.applicationDirectory + “www/” + filename doesn’t work either. I also tested with 3 different mp3 files, so the likelihood it’s an issue with the files should be small.

Any hint as to what I might be doing wrong here?

Thanks in advance.

Code snippet:

//One time:

        path = "cdvfile://localhost/www/sonar_short.mp3";
        maxProximityMedia = new Media(path,
            function () {
                log("maxProximityPlayInit(): Audio Success");
            },
            function (err) {
                log("maxProximityPlayInit(): Audio Error: " + JSON.stringify(err), true);
            });
    }

...

//Any number of times:

        if (volume > 0.0) {
            maxProximityMedia.play();
            maxProximityMedia.setVolume(volume);
        }

I notice that you aren’t calling resolveLocalFileSystemURL - the file plugin indicates you need to do this to get the proper file URL. Can you give that a try?

I’m afraid that made it not work on Android either (I followed an example in the README for cordova-plugin-file), the generated URLs caused error messages from Media, and Media is supposed to resolve this anyway. Surprising how little this is explained.

I now do this, so URLs are already used:

    let path = "";
    if (platform === "android") {
        path = "file:///android_asset/www/" + filename;
    }
    else {
        path = "cdvfile://localhost/www/" + filename;
    }

I’ll continue experimenting.

Thanks anyway.

The following works (complete code):

Due to a space in the project name there was a %20 in cordova.file.applicationDirectory that Media couldn’t handle. Hard to guess and undocumented :persevere:.

Interestingly too that AI (even GPT-4.1 that normally is pretty good) got it all wrong.

    let filename = "sonar_short.mp3";
    let path = decodeURI(cordova.file.applicationDirectory) + "www/" + filename;

    maxProximityMedia = new Media(path,
        function () {
            log("maxProximityPlayInit(): Audio Success");
        },
        function (error) {
            log("maxProximityPlayInit(): Audio Error: " + JSON.stringify(error), true);
        });

Playing a sound with volume set by BLE beacon proximity:

    if (maxProximityMedia) {
        let volume = map(maxProximity, 20, 0, 0.0, 1.0);
        if (volume > 0.0) {
            maxProximityMedia.play();
            maxProximityMedia.setVolume(volume);
        }
    }