从Cordova Media Plugin上传录制文件

jam*_*mes 2 media upload record cordova

我正在尝试上传在iOS上使用Cordova Media Plugin录制的音频文件.音频文件已创建,我可以播放它们.

但我找不到从文件系统上传录像的工作解决方案.我的录音代码是:

 record = new Media(src,
            // success callback
            function () {
                console.log("recordAudio():Audio Success");
            },

            // error callback
            function (err) {
                console.log("recordAudio():Audio Error: " + err.code);
            });

 // Record audio
 record.startRecord();
 //when finished
 record.stopRecord();
Run Code Online (Sandbox Code Playgroud)

jam*_*mes 14

我发现了怎么做:

在iOS上,您必须首先添加Cordova文件系统插件以创建新条目.

cordova plugin add org.apache.cordova.file
Run Code Online (Sandbox Code Playgroud)

要创建记录文件并设置全局变量fileURL和audioRecord:

    //Prepares File System for Audio Recording
    audioRecord = 'record.wav';
    onDeviceReady();

    function onDeviceReady() {
        window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, gotFS, fail);
    }

    function gotFS(fileSystem) {
        fileSystem.root.getFile(audioRecord, {
            create: true,
            exclusive: false
        }, gotFileEntry, fail);
    }

    function gotFileEntry(fileEntry) {
        fileURL = fileEntry.toURL();
    }
Run Code Online (Sandbox Code Playgroud)

开始和停止记录:

 record = new Media(audioRecord,
            // success callback
            function () {
                console.log("recordAudio():Audio Succes: ");
            },

            // error callback
            function (err) {
                console.log("recordAudio():Audio Error: " + err.code);
            });

        // Record audio
        record.startRecord();
        // Wait
        record.stopRecord();
Run Code Online (Sandbox Code Playgroud)

要上传文件,首先必须添加文件传输插件:

cordova plugin add org.apache.cordova.file-transfer
Run Code Online (Sandbox Code Playgroud)

然后你可以调用这个方法来上传记录:

//Method to upload Audio file to server
var uploadAudio = function () {
    var win = function (r) {
        console.log("Code = " + r.responseCode);
        console.log("Response = " + r.response);
        console.log("Sent = " + r.bytesSent);
    }

    var fail = function (error) {
        alert("An error has occurred: Code = " + error.code);
        console.log("upload error source " + error.source);
        console.log("upload error target " + error.target);
    }

    var options = new FileUploadOptions();
    options.fileKey = "file";
    options.fileName = "recordupload.wav";
    options.mimeType = "audio/wav";

    var ft = new FileTransfer();
    ft.upload(fileURL, encodeURI("http://yoururl.com/uploadaudio.php"), win, fail, options);
}
Run Code Online (Sandbox Code Playgroud)

要接收文件,您可以使用此PHP脚本:

<?php
// Where the file is going to be placed
$target_path = "records/";

/* Add the original filename to our target path.
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['file']['name']);

if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['file']['name']).
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
    echo "filename: " .  basename( $_FILES['file']['name']);
    echo "target_path: " .$target_path;
}
?>
Run Code Online (Sandbox Code Playgroud)