PhoneGap API captureVideo无法正常工作

时间:2016-03-14 12:41:49

标签: android cordova

我已经从phonegap api文档中复制并粘贴了captureVideo代码,但收到错误消息“"无法读取'捕获'未定义"我想要做的就是点击一个按钮来激活该功能然后启动相机打开,这样我就可以开始录制了。 这就是我到目前为止所做的。

    var capture = navigator.device.capture;

// 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 captureVideo() {
        // Launch device video recording application, 
        // allowing user to capture up to 2 video clips
        navigator.device.capture.captureVideo(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 });   
    }

    <!DOCTYPE html> 
<html> 
 <head>
        <title>
            Walking Tour Generator
        </title>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="msapplication-tap-highlight" content="no" />
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <link rel="stylesheet" href="jqm/jquery.mobile-1.4.5.min.css" type="text/css" />
        <link rel="stylesheet" href="jqm/jquery.mobile.icons.min.css" type="text/css" />
        <link rel="stylesheet" href="jqm/walkingtour.css" type="text/css" />
        <link rel="stylesheet" href="jqm/walkingtour.min.css" type="text/css" />
        <link rel="stylesheet" href="jqm/mycustomcss.css" type="text/css" />
        <script type="text/javascript">
</script>
        <script src="jqm/jquery.js" type="text/javascript">
</script>
        <script src="jqm/jquery.mobile-1.4.5.min.js" type="text/javascript">
</script>
        <script type="text/javascript" src="cordova.js">
</script>
        <script type="text/javascript" src="js/index.js">
</script>
        <script type="text/javascript" src="js/savingnewtour.js">
</script>
        <script type="text/javascript" src="js/addsampletours.js">
</script>
        <script type="text/javascript" src="js/loadsampletours.js">
</script>
        <script type="text/javascript" src="js/loadsavedtours.js">
</script>
        <script type="text/javascript" src="js/capturevideo.js">
</script>
        <script type="text/javascript" src="js/getcoordinates.js">
</script>


    <div data-role="content">               
                <input type="button" value="Record" id="capturevideo" onclick="captureVideo();" />
            </div>

非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

您是否仅在不安装插件的情况下将代码粘贴到项目中?你必须运行命令

cordova plugin add cordova-plugin-capture

在项目根目录的控制台中,然后才能访问navigator.device.capture。

请参阅文档以供参考: https://github.com/apache/cordova-plugin-media-capture