PhoneGap Media audio record and send to server using File Transfer

409 views
Skip to first unread message

Xylo App

unread,
Jan 4, 2016, 6:32:00 PM1/4/16
to phonegap
Hi, I'm using PhoneGap to record audio and my goal is to send the recorded file to my server when complete. I've developed the following code through what I've found so far from others that tried accomplish the same goal. The audio is successfully recorded, and I can even play it back. However, the part in which it gets sent to my server is not working. My knowledge of code is a bit limited, so I was hoping someone could take a look at the following code and see if anything jumps out as an error which would cause the upload to fail.


//  Create the record file and set variables fileURL and audioRecord   //


       
//


        audioRecord
= 'record.wav';


        document
.addEventListener("deviceready", onDeviceReady, false);



       
// Cordova is ready


       
//


       
function onDeviceReady() {


        window
.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail);


       
}



       
function onFileSystemSuccess(fileSystem) {


        fileSystem
.root.getFile(audioRecord, {


                create
: true,


                exclusive
: false


       
}, gotFileEntry, fail);


       
}



       
function gotFileEntry(fileEntry) {


        fileURL
= fileEntry.toURL();


       
}



       
function fail(fileSystem) {


        alert
("File System Error");


       
}



//  Start recording and stop after 10 seconds   //


       
//


       
function recordAudio() {


       
var record = new Media(audioRecord, onSuccess, onError);



       
// Record audio


        record
.startRecord();



       
// Stop recording


       
var recTime = 0;


       
var recInterval = setInterval(function() {


                recTime
= recTime + 1;


                setAudioPosition
(recTime + " sec");


               
if (recTime >= 10) {


                clearInterval
(recInterval);


                record
.stopRecord();


               
}


       
}, 1000);


       
}



       
// Cordova is ready


       
//


       
function onDeviceReady() {


        recordAudio
();


       
}



       
// onSuccess Callback


       
//


       
function onSuccess() {


        alert
("recordAudio():Audio Success");


        uploadAudio
();


       
}



       
// onError Callback


       
//


       
function onError(error) {


        alert
('code: '  + error.code    + '\n' +


               
'message: ' + error.message + '\n');


       
}



       
// Set audio position


       
//


       
function setAudioPosition(position) {


        document
.getElementById('audio_position').innerHTML = position;


       
}



//  Upload the recorded file to server  //


       
//


       
function uploadAudio(fileURL) {



       
var options = new FileUploadOptions();


        options
.fileKey = "file";


        options
.fileName = "recordupload.wav";


        options
.mimeType = "audio/wav";



       
var ft = new FileTransfer();


        ft
.upload(fileURL, encodeURI("http://www.xylo-app.com/images/test/i..."), win, fail, options);


       
}



       
function win(r) {


                console
.log("Code = " + r.responseCode);


                console
.log("Response = " + r.response);


                console
.log("Sent = " + r.bytesSent);


                alert
("success!");


       
}



       
function fail(error) {


                alert
("An error has occurred: Code = " + error.code);


                console
.log("upload error source " + error.source);


                console
.log("upload error target " + error.target);


       
}


Any help with this is greatly appreciated. Thanks!

Kerri Shotts

unread,
Jan 5, 2016, 11:13:40 AM1/5/16
to phonegap
Please review the welcome text for this group and define "is not working". You've shared some code -- great, but it looks to me like you're logging some additional information, so it would be a great thing to include any errors you are receiving, if any.

Also check your server-side code (posting some code here might also be useful) and logs. It might not be a PG issue, but a server-side issue.


Message has been deleted

Xylo App

unread,
Jan 5, 2016, 12:57:35 PM1/5/16
to phonegap
Hi Kerri, thanks for the reply. What I mean by not working is that nothing happens. There are no errors, but with PhoneGap and on our server. I have tried the File Transfer example of uploading an image from your device's photo library, and that one worked and showed up in my server. I just cannot seem to get it to work with this example (recorded audio). I'm thinking that I'm just not specifying the path to the file on the device correctly, but that's just my theory. Also, just in case it is the server-side code, below is the .php script. Although, like I mentioned above, the script did correctly transfer the tested image file.


<?php
    print_r
($_FILES);
    move_uploaded_file
($_FILES["file"]["tmp_name"], "/home/xyloappc/public_html/images/test/uploads/".$_FILES["file"]["name"]);
?>


Thanks again!
Reply all
Reply to author
Forward
0 new messages