【问题标题】:Cordova Media-Capture plugin is not workingCordova Media-Capture 插件不工作
【发布时间】:2021-03-18 12:29:45
【问题描述】:

我制作了一个简单的 cordova 测试应用程序来测试运行媒体捕获插件,单击录制音频、捕获图像或录制视频的按钮没有任何反应

以下是我在 cordova 项目目录中安装的插件:

以下是 HTML 文件的代码

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

以下是JS文件的代码,我在其中分别添加了音频捕获、图像捕获和视频捕获的功能。

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

【问题讨论】:

    标签: javascript cordova cordova-plugins cordova-media-plugin mediacapture


    【解决方案1】:

    Cordova 必须完全加载才能使用设备 API

    cordova 提供了 deviceready 事件

    在 HTML 正文中添加 onload 函数

    <body onload="onLoad()">
    

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

    【讨论】:

    • 您好,感谢您回答我。到目前为止,我已经这样做了,但没有成功。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-26
    • 1970-01-01
    • 2014-09-19
    • 1970-01-01
    相关资源
    最近更新 更多