Search code examples
cordovacordova-pluginsvisual-studio-cordovawindows-10-universal

Cordova Cannot Play file on Windows 10


I am using the latest verion of Cordova + VS2015.

When I try to play an mp3 file that is included as part of the app in a windows10 app, I get an error (code: 1)

The play works fine when run under iOS and Android, it's only windows 10 that seems to be the problem.

I have tried a number of variations on the path "/myfile.mp3", "myfile.mp3", "/www/myfile.mp3", "www/myfile.mp3" all with the same result. I tried a file name that does not exist and get the same error. This leads me to beleive that the path is not correct. If I print window.location.pathname, I get "/www/index.html", so, I suspect that "/www/myfile.mp3" should work.

The file has been added to the project, it is showing up in the www directory in the platforms folder.

Here is the code, basically cut right from the manual

    console.log(window.location.pathname);
    var myMedia = new Media('/www/myfile.mp3', function () {
            console.log('success');
    }, function (e) {
            console.log(e); // I always land here, where is is 'code: 1'
    }, function (e) {
            console.log(e);
    });
    myMedia.play();

Solution

  • Use "ms-appx:" scheme will solve the problem. See App Package Section of the document:

    To access files stored inside the application package, but from code where there is no inferred root authority, specify the ms-appx: scheme.

    In your case, the path should be modified to "ms-appx:///www/myfile.mp3", if current platform is windows(need to install cordova-plugin-device as pre-work here):

    var url = "";
    if (device)
    {
        if (device.platform.toLowerCase() == "windows") {
            url = "ms-appx:///www/myfile.mp3";
        } else {
            url = "/myfile.mp3";
        }
    }
    var myMedia = new Media(url, function () {
        console.log('success');
    }, function (e) {
        console.log(e); // I always land here, where is is 'code: 1'
    }, function (e) {
        console.log(e);
    });
    myMedia.play();