0

I am developing an app with JQM and then using build.phonegap.com, i am building the app.

I have a feature to download the audio file from the server and then play it in the app.

it is working perfectly in android.

I tried all possibilities to make it work.

can some one help on this.

here is my complete file handling code

fileSystem.root.getDirectory("auidofiles", {
        create : true,
        exclusive : false
    }, function(dirEntry) {     


        dirEntry.getFile(mp3_file, {
            create : false,
            exclusive : false
        }, function(fileEntry) {    

            alert("File Already downloaded");               

            if (device.platform == "Android") { 
                FILEPATH = fileEntry.fullPath;
                //FILEPATH = fileEntry.toURI();
                playAudio();                    
            } else {
                var filePathLocal1 = getPhoneGapPath() + "auidofiles/" +  mp3_file; 
                //var filePathLocal = fileSystem.root.toURL() + "auidofiles/" +  mp3_file;  
                //alert("Ios File Path:" + filePathLocal);
                //FILEPATH = filePathLocal;
                //FILEPATH = fileEntry.fullPath;
                //FILEPATH = fileEntry.toURI();
                playAudio();
            }


        }, function(fileFail) {

            alert("File Not found");

            var ft = new FileTransfer();

            ft.onprogress = function(progressEvent) {

                if (progressEvent.lengthComputable) {
                    var perc = Math.floor(progressEvent.loaded
                            / progressEvent.total * 100);
                    $('#downloadStatus').html(perc + "% loaded...");
                } else {

                    if ($('#downloadStatus').html() == "") {
                        $('#downloadStatus').html("Loading");
                    } else {
                        $('#downloadStatus').html(
                                $('#downloadStatus').html() + ".");
                    }
                }
            }; //ft.Progress

            var filePathLocal = fileSystem.root.toURL() + "auidofiles/" +  mp3_file;    
            ft.download(audioStreamUrl, filePathLocal, function(entry) {    

                //alert("File = " + entry.toURI()); 
                if (device.platform == "Android") {
                    FILEPATH = entry.fullPath;
                    //FILEPATH = entry.toURI();
                    playAudio();
                } else {
                    var filePathLocal1 = getPhoneGapPath() + "auidofiles/" +  mp3_file; 
                    //alert("Ios File Path:" + filePathLocal);
                    FILEPATH = filePathLocal;
                    //FILEPATH = entry.fullPath;
                    //FILEPATH = entry.toURI();
                    playAudio();
                }

            }, function(error) {

                $('#downloadStatus').html(
                        "download error source " + error.source);
                $('#downloadStatus').html(
                        "download error target " + error.target);
                $('#downloadStatus').html("upload error code" + error.code);

            }); //ft.Download           
        }); //getFile End

    }, function(dirfail) {
        alert("dirfail");   
        alert(JSON.stringify(dirfail))
        $('#downloadStatus').html(JSON.stringify(dirfail));
    })

function getPhoneGapPath() {

var path = window.location.pathname;
path = path.substr( 0, path.length - 10 );
return 'file://' + path;

};

  • Check my ans- http://stackoverflow.com/questions/21577230/phonegap-save-image-from-url-into-device-photo-gallery/21579097#21579097 works fine on IOS also. – Suhas Gosavi Jun 23 '14 at 07:12

2 Answers2

0

There is a new requirement to use .toURL() instead of .fullPath

This fixes the majority of these type of problems - however I have experienced a similar problem using older versions of the iOS (5.x) where filetransfer.download never fires success/fail/progress events even though it does actually download the file. I then get an app crash when I try to restart the app...

Kevin
  • 169
  • 6
-1

I had the same issue and fixed it with downgrading the Cordova Plugin FileTransfer from v0.4.2 to v0.3.3 while using Phonegap/Cordova v3.3.

smoench
  • 11
  • 4