如果<;音频>;Javascript中的源代码更改

如果<;音频>;Javascript中的源代码更改,javascript,audio,cookies,Javascript,Audio,Cookies,所以,我一直在玩弄这一点,我对JS很糟糕,但我觉得我真的很快就明白了,所以即使只是指出了正确的方向也会有所帮助 现在我有通过cookies持续播放页面变化的音频,这部分工作得很好。但是,当歌曲源更改时,我真的希望歌曲从0开始。(现在,它只是以cookie当前持有的值继续播放。) 这就是我到目前为止所做的: var song = document.getElementsByTagName("audio")[0]; var source = song.currentSrc; var original

所以,我一直在玩弄这一点,我对JS很糟糕,但我觉得我真的很快就明白了,所以即使只是指出了正确的方向也会有所帮助

现在我有通过cookies持续播放页面变化的音频,这部分工作得很好。但是,当歌曲源更改时,我真的希望歌曲从0开始。(现在,它只是以cookie当前持有的值继续播放。)

这就是我到目前为止所做的:

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);
它只是让这首歌根本无法播放

它看起来应该是正确的,我只是在这一点上困惑,为什么它不工作

有人对如何让它发挥作用有什么建议吗

编辑: 我把多余的代码移到了原来的函数中,这就是我现在所拥有的。它仍然不工作,音乐将再次播放,但不会重置cookie


cookie确实正常工作,我只是尝试更改它,看起来您在使用重置后立即将cookie中的值分配给song.currentTime

song.currentTime = 0; 
song.currentTime = tillPlayed; 
根据您的总体实施情况,您可以通过以下几种方式解决此问题,如本解决方案变体中的注释所述:

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 */
var song=document.getElementsByTagName(“音频”)[0];/*html播放器的原始源歌曲*/
var source=getCookie(“mySong”);
如果(来源)/*您可以针对您的具体用例使用不同的/附加的逻辑*/
song.currentSrc=source/*设置音频播放器的源*/
其他的
source=song.currentSrc/*保存音频播放器源*/
var originalSource=来源;
var=false;
var tillPlayed=getCookie(“timePlayed”);
函数更新()
{
如果(!播放)
{
如果(播放)
{
if(source==originalSource)/*请注意,这将仅检测自我们进入页面以来是否有音频*/
{
song.currentTime=tillPlayed;
歌曲。游戏();
播放=真实;
}
其他的
{
song.currentTime=0;
/*因为我们只是重置,所以我们不想检索cookie中的现有值*/
/*cong.currentTime=till*/
setCookie('timePlayed',song.currentTime);/*将重置值存储在cookie中。如果末尾的条件已删除,则不需要在此处执行此操作*/
setCookie('mySong',song.currentSrc);
歌曲。游戏();
播放=真实;
}
}
其他的
{
歌曲。游戏();
播放=真实;
}
}
/*如果不干扰现有的逻辑,,
考虑删除其他文件,并始终将值存储在cookie中。
*/
其他的
{
setCookie('timePlayed',song.currentTime);
setCookie('mySong',song.currentSrc);
}
}
setInterval(更新,0);/*请注意,<10ms的值解析为10ms*/

希望对您有所帮助

看起来您在使用重置值后立即将cookie中的值分配给song.currentTime

song.currentTime = 0; 
song.currentTime = tillPlayed; 
根据您的总体实施情况,您可以通过以下几种方式解决此问题,如本解决方案变体中的注释所述:

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 */
var song=document.getElementsByTagName(“音频”)[0];/*html播放器的原始源歌曲*/
var source=getCookie(“mySong”);
如果(来源)/*您可以针对您的具体用例使用不同的/附加的逻辑*/
song.currentSrc=source/*设置音频播放器的源*/
其他的
source=song.currentSrc/*保存音频播放器源*/
var originalSource=来源;
var=false;
var tillPlayed=getCookie(“timePlayed”);
函数更新()
{
如果(!播放)
{
如果(播放)
{
if(source==originalSource)/*请注意,这将仅检测自我们进入页面以来是否有音频*/
{
song.currentTime=tillPlayed;
歌曲。游戏();
播放=真实;
}
其他的
{
song.currentTime=0;
/*因为我们只是重置,所以我们不想检索cookie中的现有值*/
/*cong.currentTime=till*/
setCookie('timePlayed',song.currentTime);/*将重置值存储在cookie中。如果末尾的条件已删除,则不需要在此处执行此操作*/
setCookie('mySong',song.currentSrc);
歌曲。游戏();
播放=真实;
}
}
其他的
{
歌曲。游戏();
播放=真实;
}
}
/*如果不干扰现有的逻辑,,
考虑删除其他文件,并始终将值存储在cookie中。
*/
其他的
{
setCookie('timePlayed',song.currentTime);
setCookie('mySong',song.currentSrc);
}
}
setInterval(更新,0);/*请注意,<10ms的值解析为10ms*/

希望这对您有所帮助

您在
else
之前缺少了一个
}
。我刚刚修正了密码。也许这是你的问题之一?哦,很好,我可以试试哈哈谢谢:3编辑:可能是问题之一,但现在它仍然没有重置cookie哈哈。可能是cookie被读取为一个字符串,但currentTime需要一个数字吗?哦,你知道这很有意义,哈哈,为什么要使用cookies?如果没有服务器开销,
localStorage
难道不能做同样的工作吗?在
else
之前,您缺少了一个
}
。我刚刚修正了密码。也许这是你的问题之一?哦,太好了,我可以试试哈哈谢谢:3编辑:可能是问题之一,但现在它仍然没有重置cookie哈哈。可能是cookie被读取为字符串,但currentTime需要一个数字吗?哦,你知道吗