Development of sleep app (1): native.js calls the native mediorecorder object to realize the function of monitoring dreamtalk

Keywords: Android Mobile Javascript iOS

We are going to develop a sleep aid app. On the way, we need to realize the function of listening to the user's dreamtalk. Because the h5 + encapsulated audiocorder object used by the front desk staff is not enough to achieve the expected effect, so we decided to use native.js to call the native mediacorder object to realize the dreamtalk listening function.

The main logic of the code is to detect the decibel size of the voice through real-time recording to determine whether the audio conforms to the decibel size of the dreamtalk. If there is audio input detected, save the file to the root directory of the mobile phone. The code should be written in the timer and called regularly.

The code is as follows:

<script type="text/javascript">
    
    	//Parameter n is recording time in milliseconds
	    function sleep(n) {
	        var start = new Date().getTime();
	        //  console.log('before sleep: '+ start);
	        while (true) {
	            if (new Date().getTime() - start > n) {
	                break;
	            }
	        }
	        // console.log('after sleep: '+ new Date().getTime());
	    }
	    	
	    	var index = 0;//Identify the dreamtalk file. This variable should be written outside the timer. In order to change the file name, otherwise the file will be overwritten
	    	document.addEventListener('plusready', function(){
   			//console.log("all plus api should be called after this event happens, otherwise plus is undefined will appear. "
   			
   			var recorder = plus.ios.importClass("android.media.MediaRecorder");
   			var mrecorder = new recorder();
   			var File = plus.android.importClass("java.io.File");
   			var filePath = "/sdcard/dream"+ index++ +".mp3";
   			 // Set microphone as audio source
            mrecorder.setAudioSource(recorder.AudioSource.MIC);
            // Set the encoding of audio files
            mrecorder.setOutputFormat(recorder.OutputFormat.DEFAULT);
            // Format output file
            mrecorder.setAudioEncoder(recorder.AudioEncoder.DEFAULT);//Can be set to mediarecorder.audioencoder.amr? NB
   			
   			mrecorder.setOutputFile(filePath);

            mrecorder.prepare();
            mrecorder.start();
            mrecorder.getMaxAmplitude();
            sleep(5000);
            
            if (mrecorder!=null) {
            	//mrecorder.stop();
            	//mrecorder.release();
            	
            }
           
            var radio = mrecorder.getMaxAmplitude();//Get the maximum amplitude
            
            //Get maximum volume
            if (radio>1) {
            	radio = 20 * Math.log10(radio);
            }
            //If the decibel is greater than 30, it can be heard by the human ear, so the recording is saved
            if (radio>30&&mrecorder!=null) {
            	mrecorder.stop();
            	mrecorder.release();
            }
            if (radio<=30&&mrecorder!=null) {
            	console.log("reset");
            	mrecorder.stop();
            	mrecorder.release();
            	var file = new File(filePath);
            	if (file.exists()) {
            		file.delete();
            		console.log("Decibel small, delete file")
            	}
            }
   		});
   		
    </script>

The above code block can realize the basic function of dreamtalk monitoring. The annotation is very detailed and the logic is very simple. If you want to use it in real machine test, you must pay attention to the addition of permission module.

If you have any questions and don't want God to point out, thank you!

Posted by bloo on Fri, 24 Jan 2020 06:49:59 -0800