duanhan5388 2016-09-17 19:03
浏览 127

将音频传输到服务器cordova

I am using the cordova media plugin to record voice.Once i have recorded it. i want to send it to server.For that i am using FileUpload and FileTransfer.But i am facing problem in getting the recorded file.How to get the recorded audio file? I have been through cordova file plugin documentation,but didnt understand properly. and i found this error code 1 i use cordova and php .it not working for me can you help please.

<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条回答 默认 最新

  • doumalu9257 2016-09-19 05:14
    关注
     ft.upload(fileURI, "url", win, fail, options);
    

    In above code, "url" must be server side script lets say php script which should handle this file and should move to specified location in server.

    Please make sure that the server folder to which you are moving audio file should have necessary write permission.

    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题