user2177862
user2177862

Reputation: 1

Repeat multiple audio after playing all audios multiple times using JavaScript

I have some code written for playing audios (3 audio or more) each audio played multiple times. I want to make change so I can make all of the audios played again

Example for more explanation

I have

audio1 then audio2 then audio3

What I can make now that:

audio1 will play 3 times then
audio2 will play 3 times then
audio3 will play 3 times then stop

I don't want it to stop, I need to make it play from the beginning again audio1 3 times and audio2 3 times and audio3 3 times again for many times and then stop.

The code is here:

function startReading(1){


    var aCount = 3;

    // Start Reading - Play the Audio
    // -------------------------------------
    var repeatTimes = document.getElementById('repeatA').value;
    (function play(c){ 
        audio.play(); 
        audio.onended = function (){   

            if(c >= repeatTimes ){  
                if(i <= aCount){//and not reached the last audio
                        i++;
                        startReading(i);//play next audio
                }
            } else {//we didnt reached the third play, lets keep going...
                play(c+1);  
            }
        };
    })(1)//start with 1

}

Upvotes: 0

Views: 181

Answers (1)

user2177862
user2177862

Reputation: 1

I found the solution.

What I need to do is to check i if it is greater than the number of audio, if it's greater then start reading from the beginning and make i = 1, and make variable chk for check every time I repeat all the audio to stop if chk > (Repeated Times Required).

Simply means I will make the recursion start from the beginning.

Code will be:

var chk = 1;
function startReading(1){


var aCount = 3;

// Start Reading - Play the Audio
// -------------------------------------
var repeatTimes = document.getElementById('repeatA').value;
(function play(c){ 
    audio.play(); 
    audio.onended = function (){   

        if(c >= repeatTimes ){  
            if(i <= aCount){//and not reached the last audio
                    i++;
                    if(i <= aCount) startReading(i);
                    else if (repeatTimes > 1 && chk < repeatTimes ){
                            i = 1;
                            chk++;
                            startReading(i);
                    }
            }
        } else {//we didnt reached the third play, lets keep going...
            play(c+1);  
        }
    };
})(1)//start with 1

}

Upvotes: 0

Related Questions