Browse Source

update js

gavin.chen 2 years ago
parent
commit
8cfdd5bec0
1 changed files with 21 additions and 1 deletions
  1. 21 1
      src/flutter_sound_recorder.js

+ 21 - 1
src/flutter_sound_recorder.js

@@ -197,6 +197,25 @@ class FlutterSoundRecorder {
                 mediaStream = await navigator.mediaDevices.getUserMedia({ audio: true, video: false });
                 me.mediaStream = mediaStream;
 
+                const audioContext = new AudioContext();
+                const _audioSource = audioContext.createMediaStreamSource(mediaStream);
+                const analyser = audioContext.createAnalyser();
+                // todo: review if this values are right (set to mimic a behaviour closest to that of Android)
+                analyser.fftSize = 512;
+                analyser.minDecibels = -110;
+                analyser.maxDecibels = 0;
+                analyser.smoothingTimeConstant = 0.4;
+                _audioSource.connect(analyser);
+                const volumes = new Uint8Array(analyser.frequencyBinCount);
+
+                this.getVolumeLevel = () => {
+                        analyser.getByteFrequencyData(volumes);
+                        let volumeSum = 0;
+                        for (const volume of volumes)
+                                volumeSum += volume;
+                        return volumeSum / volumes.length;
+                }
+
 
                 //navigator.mediaDevices.getUserMedia(constraints).then
                 //(function(mediaStream)
@@ -411,9 +430,10 @@ class FlutterSoundRecorder {
                         this.timerId = setInterval
                                 (
                                         function () {
+                                                var volumeLevel = me.getVolumeLevel();
                                                 var now = new Date().getTime();
                                                 var distance = now - me.countDownDate;
-                                                me.callbackTable[CB_updateRecorderProgress](me.callback, me.deltaTime + distance, 0);
+                                                me.callbackTable[CB_updateRecorderProgress](me.callback, me.deltaTime + distance, volumeLevel);
 
                                         },
                                         this.subscriptionDuration