fusion27
fusion27

Reputation: 2636

PhoneGap attempting to upload audio media file - works iOS, file cannot be found Android

Code Handling the Accept & Upload tap event:

$('#btnAcceptUpload').live('tap',function () {

    if(isIOS){
        thisFileToUpload = fullRecordPath;
    } else {
        // thisFileToUpload = './'+fullRecordPath;          //doesn't work
        // thisFileToUpload = 'file:///'+fullRecordPath;    //doesn't work
        thisFileToUpload = fullRecordPath;                  //doesn't work
    }

    var options = new FileUploadOptions();

    msg = '';
    options.fileKey="file";

    msg += "options.fileKey = "+options.fileKey+"\n";
    options.fileName=thisFileToUpload.substr(thisFileToUpload.lastIndexOf('/')+1);

    msg += "options.fileName = "+options.fileName+"\n";
    options.mimeType='audio/wav';

    options.chunkedMode = false;

    msg += "options.mimeType = "+options.mimeType+"\n";
    msg += "thisFileToUpload = "+thisFileToUpload;

    alert(msg);

    var ft = new FileTransfer();
    ft.upload(thisFileToUpload, "http://10.0.17.121/~email/ttmovefiles.php", fileUploadSuccess, fileUploadFailure, options);
});

Success Callback:

function fileUploadSuccess(r) {
    console.log("Code = " + r.responseCode);
    console.log("Response = " + r.response);
    console.log("Sent = " + r.bytesSent);
    alert(r.response);
}

Failure Callback:

function fileUploadFailure(error){
    alert("An error has occurred: Code = " + error.code);
}

Thanks for looking.

Upvotes: 1

Views: 2088

Answers (2)

fusion27
fusion27

Reputation: 2636

Alright, alright. I figured this one out. I promise I'll come back through and tighten this one down later, but wanted to get it documented so I might end helping another.

  • in iOS to create a new piece of media you have no choice, you gotta use the File api. This same fully qualified spot in the filesystem was moved in to the same global var which the Media.play() method played nicely with... in iOS.
  • Not sure why it works this way, but with Android, Media.play() doesn't like the fully qualified path passed in to it. It just wants the filename and it apparently searches from the root.
  • File.FileTransfer.upload() always wants the fully qualified path of the asset to upload, regardless iOS or Android.

To Make this work:

I used the File API to create the file that the audio Media then uses to move the recording in to. I set 2 global vars: one for playing the audio on the device fullRecordPath and the other for uploading fullUploadPath.

Here's the function the creates the file, invokes the media API and sets the global vars that Android wants:

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem){

    fileSystem.root.getFile(recordFileName, {
        create: true,
        exclusive: false
    }, function(fileEntry){
        alert("---------> Android File " + recordFileName + " created at " + fileEntry.fullPath);
        fullRecordPath = recordFileName;
        fullUploadPath = fileEntry.fullPath;
        mediaVar = new Media(recordFileName, function(){
            alert("Android media created successfully");
        }, androidMediaCreateFailure, mediaStatusCallback); //of new Media
        onMediaCreated();
    }, androidMediaCreateFailure); //of getFile
}, androidMediaCreateFailure); //of requestFileSystem

Here's the code to play that media back

function playAudio() {
    var my_media = new Media(fullRecordPath,

        // success callback
        function () {
            console.log("playAudio():Audio Success");
        },

        // error callback
        function (err) {
            console.log("playAudio():Audio Error: " + err.code);
            exposeObject(err);
        });

    my_media.play();
}

Here's the code to upload

$('#btnAcceptUpload').live('tap',function () {

    if(isIOS){
        thisfullUploadPath = fullRecordPath;
    } else {
        thisfullUploadPath = fullUploadPath;
    }

    var options = new FileUploadOptions();

    msg = '';
    options.fileKey="file";

    msg += "options.fileKey = "+options.fileKey+"\n";
    options.fileName=thisfullUploadPath.substr(thisfullUploadPath.lastIndexOf('/')+1);

    msg += "options.fileName = "+options.fileName+"\n";
    options.mimeType='audio/wav';

    options.chunkedMode = false;

    msg += "options.mimeType = "+options.mimeType+"\n";
    msg += "thisfullUploadPath = "+thisfullUploadPath;

    alert(msg);

    var ft = new FileTransfer();
    ft.upload(thisfullUploadPath, "http://10.0.17.121/~email/ttmovefiles.php", fileUploadSuccess, fileUploadFailure, options);
});

Upvotes: 1

Drew Dahlman
Drew Dahlman

Reputation: 4972

On android you need to resolveFileSystem

window.resolveLocalFileSystemURI(FILEURI, function(msg){
// success call msg.fullPath
}, function(){
// FAIL
});

Upvotes: 0

Related Questions