使用Phonegap上传视频时,为什么应用会崩溃?

时间:2015-04-20 11:38:02

标签: android cordova

我使用此示例Upload Video Phonegap将视频上传到服务器,这是一个php脚本。我正好使用这段代码:

<!DOCTYPE html>
<html>
<head>
<title>Capture Video</title>

<script type="text/javascript" charset="utf-8" src="cordova.js"> </script>
<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 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 });
}

</script>
</head>
<body>
    <button onclick="captureVideo();">Capture Video</button> <br>
</body>
</html>

一旦我停止播放视频,然后点击“&#34;保存&#34;”,应用就会冻结并在之后崩溃。有什么不对?我已在多台设备上测试过它,因为某些设备可能无法支持但仍然可以。即使我在1秒或10秒之后停止视频,应用程序也会崩溃。奇怪的是,在应用程序崩溃后,视频位于Gallery中。

PHP脚本效果很好,因为我可以将照片发送给它并且效果很好,所以我不认为问题来自它。

请问任何建议?

1 个答案:

答案 0 :(得分:1)

好的,我刚刚在这个答案中做了更改:Phonegap video capture crashes并且应用程序不会再崩溃了,我可以看到wireshark即使vid没有被很好地接收也会发送到服务器但是那是另一个问题。

编辑:

更好地使用此功能:

function uploadFile(mediaFile) {
    var ft = new FileTransfer(),
        path = mediaFile.fullPath,
        name = mediaFile.name;
    var options = new FileUploadOptions();
    options.mimeType = "video/mpeg";
    options.fileName = name;
    options.chunkedMode = true;

    ft.upload(path,
        "http://192.154.23.51/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);
        },
        options);
}

我现在可以成功收到视频。