ios -> cordova record 录音

 

<!DOCTYPE html>
<html lang="en">
<head>
	<meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no" id="viewport" name="viewport">
	<meta charset="utf-8">
	<title></title>
 
	<script type="text/javascript" src="./js/jquery-1.11.1.js"></script> 
	
	<script language="javascript" src="./js/cordova_ios.js" > </script>
  
 	<script language="javascript" src="./cordova_plugins.js" > </script>
    
    <style type="text/css">
        input{width:200px;height:50px;display:block}
    </style>
        

</head>
<body>
    <div class="app">
        <h1>Apache Cordova</h1>
        <div id="deviceready" class="blink">
            <p id="test">操作提示</p><!--这里会显示录音操作中的各个变化-->
            <input type="button"  value="录音" id="luyin" />
            <input type="button" value="录音完成" id="luyin_finish" />
            <input type="button" value="录音播放" id="luyin_bofang" />
            <input type="button" value="录音上传 " id="luyin_upload" />
            
        </div>
    </div>
    <script type="text/javascript">
        
        var mediaRec;
        var recTimeMax = 10;
        var recTime = 0;
        
        var recordFile = "documents://myrecording.wav";
        
        var recordFIlePath='';
        
        document.addEventListener("deviceready", function onDeviceReady() {
            window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, function fail(){});
            }, false);

            var gotFS = function (fileSystem) {
                fileSystem.root.getFile("myrecording.wav",
                { create: true, exclusive: false }, //create if it does not exist
                function success(entry) {
                    recordFIlePath = entry.toURI();
                },
                function fail() {}
            );
        };




 
        
        //开始录音
        function recordAudio() {
  
           var src = recordFile;
           mediaRec = new Media(src, onSuccess, onError);
            
            // Record audio
            mediaRec.startRecord();
            // Stop recording after 10 sec
            recTime = 0;
            var recInterval = setInterval(function() {
              recTime = recTime + 1;
              if (recTime >= recTimeMax) {
                  clearInterval(recInterval);
                  mediaRec.stopRecord();
                  mediaRec.release();
              }
            }, 1000);
          
                                          
        }
        // onSuccess Callback
        //
        function onSuccess() {
            alert('success');
            console.log("recordAudio():Audio Success");
        }
        
        // onError Callback
        //
        function onError(error) {
            alert('code: '    + error.code    + '\n' +
                  'message: ' + error.message + '\n');
        }
        //==========
        
        //录音完成
        function recordFinish() {
            recTime = recTimeMax;
            
        }
        
        //播放audio
        function playAudio(url) {
             // Play the audio file at url
             var my_media = new Media(url,
             // success callback
             function() {
                console.log("playAudio():Audio Success");
             },
             // error callback
             function(err) {
                 console.log("playAudio():Audio Error: "+err);
             });
             
             // Play audio
             my_media.play();
        }
        //\\
        
        //上传
        function uloadRecord(){
            var win = function (r) {
                alert("Code = " + r.responseCode);
                console.log("Response = " + r.response);
                console.log("Sent = " + r.bytesSent);
            }
            
            var fail = function (error) {
                alert("An error has occurred: Code = " + error.code);
                console.log("upload error source " + error.source);
                console.log("upload error target " + error.target);
            }
            
            var options = new FileUploadOptions();
            options.fileKey = "file";
            options.fileName = "myrecording.wav";
            options.mimeType = "audio/wav";
            
            var ft = new FileTransfer();
            ft.upload(recordFIlePath, encodeURI("http://192.168.1.101/upload_record/index.php"), win, fail, options);
        }
        
        
        
        //为按钮绑定  touchstart(点击) 事件
        $("#luyin").click(function(){
            //实例化录音类
            recordAudio();
            //开始录音
            $("#test").html("开始录音");
        });

        //录音 完成
        $('#luyin_finish').click(function(){
            recordFinish();
        });

        //录音 播放
        $('#luyin_bofang').click(function(){
            playAudio(recordFile);
        });

        //录音 上传
        $('#luyin_upload').click(function(){
            uloadRecord()
        });

                                                                                                 
                                                                                                 
    </script>
 
</body>
</html>

 

需要添加 cordova 的 file  和 filetransfer 插件

 

 

你可能感兴趣的:(Cordova)