Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture Audio Phonegap error code 3

I'm creating an app with audio recording, i tried to implement this example code: http://docs.phonegap.com/en/3.3.0/cordova_media_capture_capture.md.html#capture.captureAudio

But when click on the button an error appears: Code error 3. According to documentation this errors appears when you exit the recording aplicacion before you record anything, but when i click on the button the recording app is not launched it goes directly to error function.

Capture video works fine.

Using Phonegap 3.0.0 or 2.9.0 with Phonegap Build.

Code:

<!DOCTYPE html>
<html>
<head>
<title>Capture Audio</title>

<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8" src="json2.js"></script>
<script type="text/javascript" charset="utf-8">

// Called when capture operation is finished
//
function captureSuccess(mediaFiles) {
    var i, len;
    for (i = 0, len = mediaFiles.length; i < len; i += 1) {
        uploadFile(mediaFiles[i]);
    }
}

// Called if something bad happens.
//
function captureError(error) {
    var msg = 'An error occurred during capture: ' + error.code;
    navigator.notification.alert(msg, null, 'Uh oh!');
}

// A button will call this function
//
function captureAudio() {
    // Launch device audio recording application,
    // allowing user to capture up to 2 audio clips
    navigator.device.capture.captureAudio(captureSuccess, captureError, {limit: 2});
}

// Upload files to server
function uploadFile(mediaFile) {
    var ft = new FileTransfer(),
        path = mediaFile.fullPath,
        name = mediaFile.name;

    ft.upload(path,
        "http://my.domain.com/upload.php",
        function(result) {
            console.log('Upload success: ' + result.responseCode);
            console.log(result.bytesSent + ' bytes sent');
        },
        function(error) {
            console.log('Error uploading file ' + path + ': ' + error.code);
        },
        { fileName: name });
}

</script>
</head>
<body>
    <button onclick="captureAudio();">Capture Audio</button> <br>
</body>

like image 938
Duver Avatar asked Jan 11 '23 11:01

Duver


1 Answers

This is not an exactly answer to your problem, but a working alternative.

Instead of using org.apache.cordova.media-capture I tried org.apache.cordova.media.

Here is a small example:

function recordAudio() {
    var src = "myrecording.amr";
    var mediaRec = new Media(src,
        // success callback
        function() {
            console.log("recordAudio():Audio Success");
        },

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

    // Record audio
    mediaRec.startRecord();

    // Stop recording after 10 seconds
    setTimeout(function() {
        mediaRec.stopRecord();
    }, 10000);
}

Unfortunaly each device uses their own audio codec and in case of Android it is the AMR Codec. I was able to listen to the record using MPC (Windows). But if you want to upload the file and share it with other users, you'll have to convert it.

If you do not find the myrecording.amr file: It is located on devices' root. I did not figure out how to store files temporary in the application cache folder itself, which is existing.

A 10 second recording has < 20kb size.

AMR 8000Hz mono 12kbps [Audio]

Which is very bad quality. Unfortunaly there seems to be no possibility to increase the quality.

like image 64
Armin Avatar answered Jan 18 '23 10:01

Armin