I am trying to make an auto-play thing. How do I make it work?

Joined
Dec 14, 2021
Messages
28
Reaction score
2
I have this code, it works fine, but it doesn't loop back to the first audio file.

JavaScript:
function autoPlayTrig() {
    var aPT = document.getElementById("autoPlay");
    if (aPT.innerHTML == "no") {
        CurAudio.play();
        aPT.innerHTML = "yes";
        playBTNlock("yes");
        CurAudio.onended = function() {
            if (curSetTrack == trackNum) {
                curSetTrack = 0;
                CurAudio.src = trackArray[curSetTrack].split("||")[0];
                nt.innerHTML = trackArray[curSetTrack].split("||")[1];
                tkLan.innerHTML = trackArray[curSetTrack].split("||")[2];
                ttn.innerHTML = curSetTrack;
               
                CurAudio.play();
            } else {
                curSetTrack += 1;
                CurAudio.src = trackArray[curSetTrack].split("||")[0];
                nt.innerHTML = trackArray[curSetTrack].split("||")[1];
                tkLan.innerHTML = trackArray[curSetTrack].split("||")[2];
                ttn.innerHTML = curSetTrack;
               
                CurAudio.play();
            }
        }
    } else if (aPT.innerHTML == "yes") {
        CurAudio.pause();
        aPT.innerHTML = "no";
        playBTNlock("no");
    }
}

As I said, it works, but when it gets to the end of the last audio file, it doesn't loop back to the first audio file.

CurAudio is the audio tag in the HTML.
nt is the name of the audio file.
tkLan is the length of the audio file.
ttn is the position the audio file is in the playlist.
trackArray is the array where all audio files and necessary data of them are held.
playBTNlock is the function that says whether or not the normal play button is disabled.

So, how do I make it loop back to the first audio file?
 
Joined
Mar 11, 2022
Messages
226
Reaction score
32
In other words, that line isn't working???
Code:
CurAudio.src = trackArray[curSetTrack].split("||")[0];

What does console log say about?
Code:
console.log(trackArray[curSetTrack].split("||")[0]);

Maybe you give that a try
Code:
CurAudio.stop();
CurAudio.src = null;
CurAudio.src = trackArray[curSetTrack].split("||")[0];
 
Joined
Dec 14, 2021
Messages
28
Reaction score
2
In other words, that line isn't working???
Code:
CurAudio.src = trackArray[curSetTrack].split("||")[0];

What does console log say about?
Code:
console.log(trackArray[curSetTrack].split("||")[0]);

Maybe you give that a try
Code:
CurAudio.stop();
CurAudio.src = null;
CurAudio.src = trackArray[curSetTrack].split("||")[0];

That's the thing, CurAudio.src = trackArray[curSetTrack].split("||")[0]; is working, but when the last audio plays it just doesn't loop back to the first, but when I play the last audio again once it ends it does go back, so I believe that the if (curSetTrack == trackNum){...} isn't being triggered, so how do I make it so that it does, as I do believe that it is my problem.

JavaScript:
CurAudio.onended = function() {
    if (curSetTrack == trackNum) {
        // this part is to loop back to first audio file when last audio file ends playing
        // but that doesn't happen, it doesn't loop back, so I believe that this part is not triggering
        curSetTrack = 0;
        CurAudio.src = trackArray[curSetTrack].split("||")[0];
        nt.innerHTML = trackArray[curSetTrack].split("||")[1];
        tkLan.innerHTML = trackArray[curSetTrack].split("||")[2];
        ttn.innerHTML = curSetTrack;
        
        CurAudio.play();
    } else {
        // code to loop through the audio files, this part works
    }
}

So I do know that the problem has nothing to do with trackArray[curSetTrack].split("||")[0], as that is working properly.
 
Joined
Mar 11, 2022
Messages
226
Reaction score
32
Sorry to say that, but i really don't understand a bit of your code.

trackNum is the number of songs. Right? curSetTrack is your current key for your songs (0 - array.length) right?

My guess is, you'll never reach the same number. I don't know how everything is generated, but i guess it should be
Code:
if (curSetTrack == (trackNum-1)) {

Best thing to find figure if curSetTrack and trackNum equals at some point is to try this

Code:
console.log('CUR SET TRACK: '+curSetTrack+'\nTrack Num: '+trackNum);
if (curSetTrack == trackNum) {

Then have a look at your console what it says.

Explanation of my guess (trackNum-1)

An array with 4 Values e.g. "Apple","Peach","Cherry","Berry" do not mean 1,2,3,4 as keys. But 0,1,2,3. As you actually already know. You do curSetTrack = 0; which is correct. An array with "Apple","Peach","Cherry","Berry" in it has a length of four. if tracknum is equal to your array.length, then curSetTrack can't reach the required number. Because as it starts on 0 and not on 1.

If that's the case, this

if (curSetTrack == (trackNum-1)) {

should work.
 
Last edited:
Joined
Mar 11, 2022
Messages
226
Reaction score
32
Ups, sorry.
You wrote:
CurAudio is the audio tag in the HTML.
nt is the name of the audio file.
tkLan is the length of the audio file.
ttn is the position the audio file is in the playlist.
trackArray is the array where all audio files and necessary data of them are held.
playBTNlock is the function that says whether or not the normal play button is disabled.
which already explains your code. Okay. Then as i wrote,

THIS
Code:
if (curSetTrack == (trackNum-1)) {
will solve your problem. Explanation is above.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top