Phonegap - 如何在使用Phonegap的media.startRecord录制后上传音频文件 [英] Phonegap - How can I upload an audio file after recording with Phonegap's media.startRecord

查看:395
本文介绍了Phonegap - 如何在使用Phonegap的media.startRecord录制后上传音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Phonegap做了标准的录音:

I have done the standard sound recording with Phonegap:

function recordSound() {

    var src = "mysound.mp3";
    var mediaRec = new Media(src, onSuccess, onError);

    // Record audio
    mediaRec.startRecord();

    // Stop recording after 10 sec
    var recTime = 0;
    var recInterval = setInterval(function() {
        recTime = recTime + 1;
        setAudioPosition(recTime + " sec");
        if (recTime >= 10) {
            clearInterval(recInterval);
            mediaRec.stopRecord();
        }
    }, 1000);

}



我现在要上传此文件(mysound.mp3)而不让用户必须选择自己。任何帮助将非常感激。

I want to now upload this file (mysound.mp3) without letting the user have to select themselves. Any help will be greatly appreciated.

到目前为止我已经做了:

So far I have done:

function win(r) {
    console.log("Code = " + r.responseCode);
    console.log("Response = " + r.response);
    console.log("Sent = " + r.bytesSent);
}

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

function up() {// !! Assumes variable fileURI contains a valid URI to a text
    // file on the device

    var fileURI = "/mnt/sdcard/mysound.mp3";
    var options = new FileUploadOptions();
    options.fileKey = "file";
    options.fileName=fileURI.substr(fileURI.lastIndexOf('/')+1);
    options.mimeType = "audio/mp3";

    var params = new Object();
    params.value1 = "test";
    params.value2 = "param";

    options.params = params;

    var ft = new FileTransfer();
    ft.upload(fileURI, "http://myserver/upload.php", win, fail, options);
}

我得到: java.io.IOException:Received从服务器

JSCallback错误:请求失败

很高兴为您提供帮助。

推荐答案

您可以使用此函数上传文件

You can use this function to upload the file

function uploadVoice(fileName, dirName, fileMime, uploadURL) {

    var win = function (r) {
        console.log("Code = " + r.responseCode);
        console.log("Response = " + r.response);
        console.log("Sent = " + r.bytesSent);
    };

    // upload failure
    var fail = function(error) {
        alert("An error has occurred: Code = " + error.code);
    };

    // file system fail
    var fsFail = function(error) {
        alert("failed with error code: " + error.code);

    };

    var dirFail = function(error) {
        alert("Directory error code: " + error.code);

    };

    var fileURI;

    var gotFileSystem = function (fileSystem) {
        fileSystem.root.getDirectory(dirName, {
            create: false
        }, function (dataDir) {

            fileURI = dataDir.fullPath;
            fileURI = fileURI + '/' + fileName;

            var options = new FileUploadOptions();
            options.fileKey = "file";
            options.fileName = fileURI.substr(fileURI.lastIndexOf('/') + 1);
            options.mimeType = fileMime;

            var params = new Object();
            params.value1 = "test";
            params.value2 = "param";

            options.params = params;

            var ft = new FileTransfer();
            ft.upload(fileURI, uploadURL, win, fail, options);

        }, dirFail);

    };

    // get file system to copy or move image file to
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFileSystem, fsFail);

}

希望这有助于

这篇关于Phonegap - 如何在使用Phonegap的media.startRecord录制后上传音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆