Search code examples
cordovaaudiocordova-pluginsaudio-recordingripple

Record audio from smartphone with Cordova


I'm very new with Cordova, and I'm trying to create a very basic app to record audio from smartphone.

So I added the plugin cordova-plugin-media-capture using VS 2015.
Then, I copy/paste this full example from the doc :

<head>
<title>Capture Audio</title>

<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
//I just remove the following line in MY code, I don't know what it corresponds
<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>

However, when I run this code and click on "Capture Audio" button, I have the following result :

capture.captureAudio fail

I also the following error in the console :

missing exec:Capture.captureAudio

TypeError: emulator[service][action] is not a function
at module.exports.exec (ripple.js:41)
at _capture (capture.js:52)
at Capture.captureAudio (capture.js:71)
at captureAudio (index.html:60)
at HTMLButtonElement.onclick (index.html:91)

I don't really understand where the problem is...

Thank you !


Solution

  • You're trying to test your app with the ripple emulator, which is just an emulator and does not simulate all device capabilities, such as recording an audio file. See also this answer.

    Try using a real device to test your app.

    Edit: I'm not sure if any emulator can actually capture sounds through your PC-hardware. The Android emulator for example can not.

    The Android Emulator does not have the ability to capture audio, but actual devices are likely to provide these capabilities.

    Taken from the adroid.developer.com MediaRecorder documentation