1

so, i've been toying with this for a little bit and i am awful with JS but i feel like I'm really close to figuring it out, so even just being pointed in the right direction would help.

Right now i have audio that plays persistently across page changes via cookies, and that part is working great. however when the song source changes, I would really like the song to start back at 0. (Right now it just keeps playing at the value the cookie currently holds.)

This is what I have so far:

var song = document.getElementsByTagName("audio")[0];
var source = song.currentSrc;
var originalSource = source;
var played = false;
var tillPlayed = getCookie("timePlayed");
function update()
{
    if(!played){
        if(tillPlayed){
        if (source == originalSource){
                song.currentTime = tillPlayed;
                song.play();
                played = true;
            }
            else {
            song.currentTime = 0;
            cong.currentTime = tillPlayed;
            song.play();
            played = true;                  
            }
    }
    else {
        song.play();
        played = true;
    }
    }

    else {
    setCookie('timePlayed', song.currentTime);
    }
}
setInterval(update,0);

And it just makes the song not play at all.

It looks like it should be correct, and i'm just at a loss at this point as to why it's not working.

Anyone have any tips on getting it to function?

EDIT: Ive moved the extra code into the original function, this is what I have at this point. It's still not working, the music will play again, but it doesn't reset the cookie.

The cookie does function as it should, I am just trying to change it

1 Answers1

0

It looks like you are assigning the value from the cookie to song.currentTime immediately after resetting it with

song.currentTime = 0; 
song.currentTime = tillPlayed; 

Depending on your overall implementation you can solve that a couple ways as described in the comments in this variant of your solution:

var song = document.getElementsByTagName("audio")[0];/* orgiinal source song of the html player */
var source = getCookie("mySong");
if (source) /* You may different/additional logic for your exact use case*/
    song.currentSrc = source /*Set audio player's source */
else    
    source = song.currentSrc; /*Save audio player source */
var originalSource = source;
var played = false;
var tillPlayed = getCookie("timePlayed");
function update()
{
    if(!played)
    {
        if(tillPlayed)
        {
            if (source == originalSource) /* Note this this will only detect if the audio has since we came into the page. */
            {
                song.currentTime = tillPlayed;
                song.play();
                played = true;
            }
            else 
            {
                song.currentTime = 0;
                /* Since we just reset we do not want to retrieve the existing value in the cookie*/
                /* cong.currentTime = tillPlayed; */
                setCookie('timePlayed', song.currentTime);  /* Store the reset value in the cookie.  If the condition at the end is removed, you don't need to do this here. */
                setCookie('mySong', song.currentSrc);
                song.play();
                played = true;                  
            }
        }
        else 
        {
            song.play();
            played = true;
        }
    }
    /* If it does not interfere with existing logic, 
    consider removing the else and always storing the value in the cookie here. 
    */
    else 
    {
        setCookie('timePlayed', song.currentTime);
        setCookie('mySong', song.currentSrc);
    }
}
setInterval(update,0); /* Note that values of < 10ms resolve to 10ms */

Hope that helps

codemonk
  • 93
  • 7
  • yeah it still doesn't seem to work. I wonder if on page change that the originalSource variable is just automatically getting replaced with the new source from the source variable? – Beloise Michelle Huffstutler May 13 '20 at 22:00
  • Assuming the JS is just included on each page, no state would be persisted between them so source will be set to song.currentSrc. What action triggers the song to change? Does a song change trigger a page navigation? – codemonk May 14 '20 at 02:59
  • yeah it's on every page. On certain pages, I have a different audio source in the HTML5 audio player, which is what changes it currently. – Beloise Michelle Huffstutler May 14 '20 at 12:34
  • Is it not working by starting the original song over now (instead of resuming)? I think you would need to save your current song as well. I updated the example to include that. You may also need to save your original source depending on which audio source you want to base whether or not it has changed on, but I think this will get you close. – codemonk May 14 '20 at 18:30
  • yeah this looks really close to what i need tbh, Ill add in the specific code case stuff and play around with it, feeling a bit out of it right now, but thank you so much for helping me out with this. Ill be sure to let you know if i get it working from here! – Beloise Michelle Huffstutler May 14 '20 at 18:52
  • No problem! Please mark answer as correct if it helped you get what you needed. Once you have it working, please consider the alternate approach suggested by @terrymorse. More info here: https://stackoverflow.com/questions/3220660/local-storage-vs-cookies – codemonk May 19 '20 at 02:19