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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)