如何使用cordova录制后获取音频文件

时间:2015-06-22 07:39:55

标签: cordova

我正在使用cordova媒体插件录制语音。一旦我录制了它。我想将它发送到server.For我正在使用FileUpload和FileTransfer.But我面临着获取录制文件的问题。如何获取录制的音频文件?我已经通过cordova文件插件文档,但没有正确理解。

  <html>   
    <head>
        <script src="cordova.js"></script>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

    <script>
                var filename;         
                var mediaRec;



            function recordAudio() {
                    filename = "myrecording "+new Date().getTime()+".amr";

                    mediaRec = new Media(filename, onSuccess, onError);
                    mediaRec.startRecord();
            }
             function onSuccess(){}
             function onError(){}
            function stopRecord(){
                 mediaRec.stopRecord();
                 sendAudio();

            }

            function sendAudio(){

              var options = new FileUploadOptions();
                                options.fileKey = "attachmentFileName";
                                options.fileName = filename;
                                options.mimeType="audio/AMR";


                                new FileTransfer().upload(filename,encodeURI("my url"), win, fail, options);


            }
function win(r) {
   alert("success");
    }
   function fail(error) {
     alert("fail");  
     alert(error.code);
    }
        </script>
    </head>
    <body>

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

    </body>
    </html>

1 个答案:

答案 0 :(得分:0)

它的工作

<html>
<head>

    <script src="cordova.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script>

        var filename;         
        var mediaRec;
         function recordAudio() {
        filename = "myrecording "+new Date().getTime()+".3gpp";

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


            }
        }, 1000);*/
    }
    function onSuccess(){}
    function onError(){}
     function stopRecord(){
         mediaRec.stopRecord();
         uploadVoice(filename,"");



    }

        function uploadVoice(fileName, dirName) {

    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");
        alert(error.code);
    };

    // file system fail
    var fsFail = function(error) {
        alert("file system error");

    };

    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 = "fileKey";
            options.fileName = fileURI.substr(fileURI.lastIndexOf('/') + 1);
            options.mimeType = "Audio/3gpp";
            options.chunkedMode = false;

            var params = new Object();
            params.name = value;
            options.params = params;

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

        }, dirFail);

    };

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

}


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

</body>

</head>
</html>