I am making a custom audio player with javascript. In this I need to knoe the amount of data buffered. For this I am using the following code:
$("#active-song")[0].addEventListener('loadedmetadata', function() {
$("#active-song")[0].addEventListener('progress', function() {
songs.duration = $("#active-song")[0].duration ;
var buff786 = $("#active-song")[0].buffered.end(0);
var percent-buffered = (buff786/songs.duration) * 100;
$(".song-progress .current-progress").css("width", percent-buffered + "%");
});
});
The problem is that before anything could happen I get an error in the console. The error exists because when the javascript code is parsed by the browser the song hasn't yet been buffered. So the var buf786
remain undefined and I get the error: Uncaught SyntaxError: Unexpected token
. I tried many aliters such as:
var buff786 = $("#active-song")[0].buffered.end(0);
if (buff786 == undefined) {
buff786 = 1;
}
var percent-buffered = (buff786/songs.duration) * 100;
Still I get the error. So how do I bypass this error. Can I parse expressions like non-existing-random + 100
in javascript?