【问题标题】:Upload Recording from Cordova Media Plugin从 Cordova 媒体插件上传录音
【发布时间】:2015-01-03 15:38:44
【问题描述】:

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

但我找不到从文件系统上传录音的有效解决方案。我的录音代码是:

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

【问题讨论】:

    标签: cordova upload media record


    【解决方案1】:

    我发现了怎么做:

    在 iOS 上,您必须先添加 Cordova 文件系统插件才能创建新条目。

    cordova plugin add org.apache.cordova.file
    

    创建记录文件并设置全局变量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();
        }
    

    开始和停止记录:

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

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

    cordova plugin add org.apache.cordova.file-transfer
    

    然后就可以调用这个方法上传记录了:

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

    要接收文件,您可以使用这个 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;
    }
    ?>
    

    【讨论】:

    【解决方案2】:

    使用文件传输插件https://github.com/apache/cordova-plugin-file-transfer/blob/master/doc/index.md。要安装:

    cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-file-transfer.git
    

    然后这样做

    var ft = new FileTransfer();
    
    var success = function (r) {
        // success code here
    };
    var fail = function (error) {
        // failure code here
        alert('File could not be uploaded');
    };
    
    options.fileKey = 'image'; // set the key for the server to know where the cotent is
    options.fileName = '/myfile.jpg'; // location of your file
    options.mimeType = "image/jpeg"; // mimeType
    
    params.myParam = 'My Value'; // add other parameters if required
    
    options.params = params;
    ft.upload(fileURI, encodeURI('/path/to/my/post/handler'), success, fail, options);
    

    【讨论】:

    • 谢谢,我的问题是获取音频文件的正确位置。
    • 应该只是src
    • @unobf,上传正常,但文件已损坏。知道为什么会这样吗?
    • @Ionut 你用的是什么 mime 类型?
    • 我正在上传一个音频文件。 mimetype 是音频/AMR。我也尝试过用于 mp3 的音频/mpeg。当我使用在线转换器工具将 amr 文件转换为 mp3 文件时,音频工作正常。知道为什么吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2015-10-31
    相关资源
    最近更新 更多