Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7977047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T09:04:52+00:00 2026-06-04T09:04:52+00:00

I’ve tried to really look this one up before asking a question but it

  • 0

I’ve tried to really look this one up before asking a question but it looks like I am in a bind here and would like to see if someone knows a clear definite answer to this problem. I’ve seen like one or two similar questions but they didn’t seem to point out my problem or at least I didn’t understand what they were going for.

I am working on an RPG system audio PHP function that plays music when called on and it uses the audio tag if the user doesn’t have their music option on mute. (in the function $song is the song thrown into the first argument.) Now if this function is called to have resume to be true then it uses some javascript. Excuse the code as it is a bit messy (to me) but here’s the following code that includes the javascript code:

function playmusic($song, $resume=false, $loop=true)
 {
  global $baseurl, $loggeduser;

 if ($resume == true)
  {
   $buttons = "
    <button onclick=\"resetplaytime()\" type=\"button\">Reset</button>
    <button onclick=\"alertplaytime()\" type=\"button\">Alert Time</button>";
   $resume = "
    <script type=\"text/javascript\">
      var curmusic_$song = document.getElementById(\"music_$song\");
      var musictime = getcookie(\"musicplaytime_$song\");

     if (musictime != null && musictime != \"\")
      {
       alert(musictime);
       curmusic_$song.currentTime = musictime; 
      }

      window.onload=settimecookie();

     function getcookie(c_name)
      {
       var i,x,y,ARRcookies=document.cookie.split(\";\");
      for (i=0;i<ARRcookies.length;i++)
       {
        x=ARRcookies[i].substr(0,ARRcookies[i].indexOf(\"=\"));
        y=ARRcookies[i].substr(ARRcookies[i].indexOf(\"=\")+1);
        x=x.replace(/^\s+|\s+$/g,\"\");
       if (x==c_name)
        {
         return unescape(y);
        }
       }
      }

     function settimecookie()
      {
       var playtime = curmusic_$song.currentTime;
      if (playtime != null && playtime != \"\")
       {
        document.cookie = 'musicplaytime_$song=' + playtime;
       }

       setTimeout(\"settimecookie()\", 50);
      }

     function alertplaytime()
      {
       alert(musictime);
      }

     function resetplaytime()
      {
       curmusic_$song.currentTime=0;
      }

    </script>";
  }

 if ($loop == true)
  {
   $songloop = " loop=\"true\"";
  }

 if ($loggeduser['mutemusic'] == 0)
  {
   return "
    </table>
    $buttons
    <audio id=\"music_$song\" autoplay=\"true\"$songloop preload=\"true\">
      <source src=\"$baseurl/boardfiles/effects/$song.ogg\" type=\"audio/ogg\" />
      <source src=\"$baseurl/boardfiles/effects/$song.mp3\" type=\"audio/mp3\" />
    </audio>
    $resume
    <table class=\"brdr\" cellpadding=\"0\">";
  }
 }

Now if I take the alert out of the loop that sets the audio tag’s playtime to the cookie value, the whole script doesn’t work (as in nothing being done) unless you use a button to call one of the functions directly and then that function works.

I’ve tried a few things from making this a function and calling it directly (it seems to only work with onclick events but if you expect it to run while onload or some automatic event then it fails.), moving it around later in the function (along with the onload for settimecookie() as it has to be after this because it will set the cookie to the current playtime), and now I am out of ideas.

I am hoping to get an answer for this as I am sure this code could help others who’ve been trying to get some sort of resume audio function that isn’t bulky or confusing to use. 🙂
I’ve been trying to find a solution for quite awhile and never have been this close. Thank you for reading. If you’d like the whole PHP function then I’ll show that too but there’s really nothing to see except two debug buttons (Reset and Alert cookie) and the audio tag with the id ‘music_$song’.

Edit: Just to be clear, in the code, there are escapes before double quotes because the javascript is being returned in the PHP function.

Edit 2: Have now bolden this edit because it has been missed by some people. In addition I have added the PHP portion.

Last edit: I have now seemed to figure something out that got me closer to what I wish to achieve. I changed the above code to the following and it works flawless for me.

function playmusic($song, $resume=false, $loop=true)
 {
  global $baseurl, $loggeduser;

 if ($resume == true)
  {
   $resume = "
    <!--
    <button onclick=\"resetplaytime();\" type=\"button\">Reset</button>
    <button onclick=\"alertplaytime();\" type=\"button\">Alert Time</button>
    <button onclick=\"settimecookie();\" type=\"button\">Start Cookie tracking</button>
    -->

    <script type=\"text/javascript\">
     function getcookie(c_name)
      {
       var i,x,y,ARRcookies=document.cookie.split(\";\");
      for (i=0;i<ARRcookies.length;i++)
       {
        x=ARRcookies[i].substr(0,ARRcookies[i].indexOf(\"=\"));
        y=ARRcookies[i].substr(ARRcookies[i].indexOf(\"=\")+1);
        x=x.replace(/^\s+|\s+$/g,\"\");
       if (x==c_name)
        {
         return unescape(y);
        }
       }
      }

     function settimecookie()
      {
       var curmusic_$song = document.getElementById(\"music_$song\");
       var playtime = curmusic_$song.currentTime;
      if (playtime != null && playtime != \"\")
       {
        document.cookie = \"musicplaytime_$song=\" + playtime;
       }

       setTimeout(\"settimecookie()\", 50);
      }

     function resumeplaytime()
      {
       var curmusic_$song = document.getElementById(\"music_$song\");
       var musictime = getcookie(\"musicplaytime_$song\");
      if (musictime != null && musictime != \"\")
       {
        curmusic_$song.currentTime = musictime; 
       }
       curmusic_$song.play(); 

       settimecookie();
      }

      window.onload=resumeplaytime;

     function alertplaytime()
      {
       var musictime = getcookie(\"musicplaytime_$song\");
       alert(musictime);
      }

     function resetplaytime()
      {
       var curmusic_$song = document.getElementById(\"music_$song\");
       curmusic_$song.currentTime=0;
      }

    </script>";
  }
 else
  {
   $autoplay = "autoplay=\"true\"";
  }

 if ($loop == true)
  {
   $songloop = " loop=\"true\"";
  }

 if ($loggeduser['mutemusic'] == 0)
  {
   return "
    </table>
    $resume
    <audio id=\"music_$song\" $autoplay$songloop preload=\"true\">
      <source src=\"$baseurl/boardfiles/effects/$song.ogg\" type=\"audio/ogg\" />
      <source src=\"$baseurl/boardfiles/effects/$song.mp3\" type=\"audio/mp3\" />
    </audio>
    <table class=\"brdr\" cellpadding=\"0\">";
  }
 }

It seems to work perfectly now. I changed a few things and now works as I want it to. Thanks for the help! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T09:04:55+00:00Added an answer on June 4, 2026 at 9:04 am

    It looks like you try to execute document.getElementById("music_$song") before the DOM is available. Use an onload handler for that.

    Yet, I could not find any call to alert() or alertplaytime() that would let that wait – especially not in a loop as you have described.

    Also, the line window.onload=settimecookie(); is buggy. settimecookie does not return an event handler function when beeing executed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.