javascriptcordovacordova-pluginscordova-media-pluginmediacapture

Cordova Media-Capture plugin is not working


i have made a simple cordova test app to test run the media-capture plugin, nothing happens on the button click of either record audio, capture image or record video

Following are the plugins i have installed in cordova project directory: enter image description here

Following is the code for HTML file

 <button id = "audioCapture">AUDIO</button>
 <button id = "imageCapture">IMAGE</button>
 <button id = "videoCapture">VIDEO</button>

Following is the code for JS file in which i'added the functionality of Audio capture, Image capture and Video capture respectively.

document.getElementById("audioCapture").addEventListener("click", audioCapture);
document.getElementById("imageCapture").addEventListener("click", imageCapture);
document.getElementById("videoCapture").addEventListener("click", videoCapture);
function audioCapture() {
    var options = {
        limit: 1,
        duration: 10
    };
    navigator.device.capture.captureAudio(onSuccess, onError, options);

    function onSuccess(mediaFiles) {
        var i, path, len;
        for (i = 0, len = mediaFiles.length; i < len; i += 1) {
            path = mediaFiles[i].fullPath;

        }
    }

    function onError(error) {
        navigator.notification.alert('Error code: ' + error.code, null, 'Capture Error');
    }
}

function imageCapture() {
    var options = {
        limit: 1
    };
    navigator.device.capture.captureImage(onSuccess, onError, options);

    function onSuccess(mediaFiles) {
        var i, path, len;
        for (i = 0, len = mediaFiles.length; i < len; i += 1) {
            path = mediaFiles[i].fullPath;

        }
    }

    function onError(error) {
        navigator.notification.alert('Error code: ' + error.code, null, 'Capture Error');
    }
}

function videoCapture() {
    var options = {
        limit: 1,
        duration: 10
    };
    navigator.device.capture.captureVideo(onSuccess, onError, options);

    function onSuccess(mediaFiles) {
        var i, path, len;
        for (i = 0, len = mediaFiles.length; i < len; i += 1) {
            path = mediaFiles[i].fullPath;

        }
    }

    function onError(error) {
        navigator.notification.alert('Error code: ' + error.code, null, 'Capture Error');
    }
}

Solution

  • Cordova must be fully loaded to use the device API

    For that cordova provides the deviceready events

    In HTML body add onload function

    <body onload="onLoad()">
    

    in JS

    function onLoad() {
      document.addEventListener("deviceready", onDeviceReady, false);
    }
    
    // ready to use device APIs
    function onDeviceReady() {
       console.log(navigator.device);
       // document.getElementById("audioCapture").addEventListener("click", audioCapture);
    }