将音频传输到服务器cordova

时间:2016-09-17 19:03:32

标签: php cordova

我正在使用cordova媒体插件录制语音。一旦我录制了它。我想将它发送到server.For我正在使用FileUpload和FileTransfer.But我面临着获取录制文件的问题。如何获取录制的音频文件?我已经通过cordova文件插件文档,但没有正确理解。我发现这个错误代码1我使用cordova和php。它不适合我,你可以帮忙吗。

 
<html>
<head>
    <script src="cordova.js"></script>
    <script src="mvc/js/jquery.min.js"></script>
    <script>
            var filename;         
        var mediaRec;
         function recordAudio() {
        filename = new Date().getTime();

        mediaRec = new Media(filename, onSuccess, onError);
        mediaRec.startRecord();


    }
    function onSuccess(){

        alert("onSuccess  "   +filename );   
    }
    function onError(){}
     function stopRecord(fileName){

         mediaRec.stopRecord();
        uploadVoice(filename,"");


    }


        function uploadVoice(fileName, dirName) {

    var win = function (r) {

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

    var fail = function(error) {

        alert("fail  "+error.code );
        alert("Source = " + error.source );
        alert("Target = " + error.target)
};

    // file system fail
    var fsFail = function(error) {
        alert("file system error");
  //alert("fsfail  "+error.code);
    };

    var dirFail = function(error) {
        alert("Directory error");


    };

    var fileURI;

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

        }, function (dataDir) {

            fileURI = dataDir.toURL();
          fileURI = fileURI + '/' + fileName;

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

            options.chunkedMode = false;
options.headers = { Connection: "close" }; 

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


        }, dirFail);

    };


    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFileSystem, fsFail);

}

    </script>
<body>
    <button onClick="recordAudio();">Start</button><br/><br/>
    <button onClick="stopRecord();">Stop</button>

</body>

</head>

1 个答案:

答案 0 :(得分:0)

Sheet2

在上面的代码中,&#34; url&#34;必须是服务器端脚本让我们说PHP脚本应该处理这个文件,并应移动到服务器中的指定位置。

请确保您要移动音频文件的服务器文件夹应具有必要的写入权限。