Skip to content

Commit 702f99f

Browse files
committed
enhance and optmize
call cancelAnimationFrame after the sound finished to prevent the potential memory insrease and reduce the CPU cost
1 parent 9b28fb6 commit 702f99f

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

js/html5_audio_visualizer.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ var Visualizer = function() {
2121
this.infoUpdateId = null, //to sotore the setTimeout ID and clear the interval
2222
this.animationId = null,
2323
this.status = 0, //flag for sound is playing 1 or stopped 0
24-
this.forceStop = false
24+
this.forceStop = false,
25+
this.allCapsReachBottom = false
2526
};
2627
Visualizer.prototype = {
2728
ini: function() {
@@ -134,8 +135,10 @@ Visualizer.prototype = {
134135
audioBufferSouceNode.stop = audioBufferSouceNode.noteOff //in old browsers use noteOn method
135136
};
136137
//stop the previous sound if any
137-
if (this.source !== null) {
138+
if (this.animationId !== null) {
138139
cancelAnimationFrame(this.animationId);
140+
}
141+
if (this.source !== null) {
139142
this.source.stop(0);
140143
}
141144
audioBufferSouceNode.start(0);
@@ -169,13 +172,24 @@ Visualizer.prototype = {
169172
var drawMeter = function() {
170173
var array = new Uint8Array(analyser.frequencyBinCount);
171174
analyser.getByteFrequencyData(array);
175+
if (that.status === 0) {
176+
//fix when some sounds end the value still not back to zero
177+
for (var i = array.length - 1; i >= 0; i--) {
178+
array[i] = 0;
179+
};
180+
allCapsReachBottom = true;
181+
for (var i = capYPositionArray.length - 1; i >= 0; i--) {
182+
allCapsReachBottom = allCapsReachBottom && (capYPositionArray[i] === 0);
183+
};
184+
if (allCapsReachBottom) {
185+
cancelAnimationFrame(that.animationId);//since the sound is top and animation finished, stop the requestAnimation to prevent potential memory leak,THIS IS VERY IMPORTANT!
186+
return;
187+
};
188+
};
172189
var step = Math.round(array.length / meterNum); //sample limited data from the total array
173190
ctx.clearRect(0, 0, cwidth, cheight);
174191
for (var i = 0; i < meterNum; i++) {
175192
var value = array[i * step];
176-
if (that.status === 0) {
177-
value = 0; //fix when some sounds end the value still not back to zero
178-
};
179193
if (capYPositionArray.length < Math.round(meterNum)) {
180194
capYPositionArray.push(value);
181195
};
@@ -190,7 +204,7 @@ Visualizer.prototype = {
190204
ctx.fillStyle = gradient; //set the filllStyle to gradient for a better look
191205
ctx.fillRect(i * 12 /*meterWidth+gap*/ , cheight - value + capHeight, meterWidth, cheight); //the meter
192206
}
193-
this.animationId = requestAnimationFrame(drawMeter);
207+
that.animationId = requestAnimationFrame(drawMeter);
194208
}
195209
this.animationId = requestAnimationFrame(drawMeter);
196210
},
@@ -201,7 +215,6 @@ Visualizer.prototype = {
201215
return;
202216
};
203217
this.status = 0;
204-
console.log('audio ended');
205218
var text = 'HTML5 Audio API showcase | An Audio Viusalizer';
206219
document.getElementById('fileWrapper').style.opacity = 1;
207220
document.getElementById('info').innerHTML = text;

0 commit comments

Comments
 (0)