简体   繁体   中英

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: 在此处输入图像描述

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');
    }
}

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM