Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-13-2013, 09:29 PM   PM User | #1
Entity_
New Coder

 
Join Date: Dec 2012
Location: Ingerlund
Posts: 62
Thanks: 8
Thanked 4 Times in 4 Posts
Entity_ is an unknown quantity at this point
CurrentTime, very strange behaviour

I'm try to make an event fire on a certain current time, but for some reason the event would not fire. To check what the problem was, I used the following code and monitored the console to see the results:

Code:
<script>

    var Sound1 = document.getElementById("player");
    if (Sound1.play) {
        alert("it's playing");
    }
    setInterval(function () { console.log(Sound1.currentTime); }, 250)

</script>

So, I first added the currentTime monitor to tell me if the time went above the thresh hold that I want to fire the event I mentioned, but It never goes above zero on the console. It stays on zero. The sound must not be playing, right? Wrong. I added the code above the interval to see if the sound actually plays, and it does! I must have made a basic error along the way but I can't see what it is! Can anyone please help me?
Entity_ is offline   Reply With Quote
Old 02-13-2013, 09:53 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,249
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Well, the most obvious thing: Does Sound1.currentTime actually produce any values, at all? Are you using the right property?

Where are the docs for the player you are using? What do they say?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 02-13-2013, 10:06 PM   PM User | #3
Entity_
New Coder

 
Join Date: Dec 2012
Location: Ingerlund
Posts: 62
Thanks: 8
Thanked 4 Times in 4 Posts
Entity_ is an unknown quantity at this point
Code:
<audio id="player" autoplay loop>
  <source src="assets/birds.mp3" type="audio/mpeg">
  <source src="assets/birds.ogg" type="audio/ogg">
  <source src="assets/birds.wav" type="audio/wav">
Your browser does not support the audio element.
</audio>
The player I'm using is just a basic HTML5 audio element. As for sound1.currentTime, no it doesn't seem to produce a value above 0 unless I misunderstand your question. I'm not sure why this is happening though, as I've used the value before with no problems.
Entity_ is offline   Reply With Quote
Old 02-13-2013, 10:33 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,249
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
I know it *shouldn't* matter, but...

Try this:
Code:
<script type="text/javascript">
function Check( )
{
    var p = document.getElementById("player");
    alert( "player is playing? " + p.play 
           + ", has been playing " + p.currentTime );
}
setInterval( Check, 1000 );
</script>
And if that works then try changing alert to console.log and if that works I think you are home free.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
Entity_ (02-13-2013)
Old 02-13-2013, 11:42 PM   PM User | #5
Entity_
New Coder

 
Join Date: Dec 2012
Location: Ingerlund
Posts: 62
Thanks: 8
Thanked 4 Times in 4 Posts
Entity_ is an unknown quantity at this point
Ok so now I know my audio is playing, thankyou very much. But my original post eluded to another problem, for which I created that code snippet. If you'd be so kind and if you have the time maybe you could help me, because the result of actually knowing that the file is playing is even more confusing.

Code:
    
    var birdSound = document.getElementById("player");
    var coverBirds = document.getElementById("coverBirds");
    var trigger = 2;
    
    

    birdSound.addEventListener('timeupdate', function () {
            console.log(birdSound.currentTime);
            if (coverBirds.paused) {
            if (birdSound.currentTime > trigger ) {
                alert("eventalert");
                birdSound.play
           }
     }
});
There is the code. It basically plays one audio element (coverBirds) at a specific time in another audio element (birdSound). It does this by checking if coverBirds is not playing, if it's not playing it checks to see if the time of birdSound has passed the allowed thresh hold set by the trigger variable. It's set up in this way so the timeupdate will not spam the sound file upon the condition being met. The problem is somewhere is the event listener i think. I think this because I added the console.log line to see if it was firing properly and instead it fired twice and that's it. Is there something I'm doing wrong?

P.S Thanks in advance for your time, if you decide to help
Entity_ is offline   Reply With Quote
Old 02-14-2013, 01:11 AM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,249
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Well, see, my *GUESS* was that for some reason when you got the reference to the playing sound via
Code:
    var birdSound = document.getElementById("player");
that for some reason that was getting the state of the player *AT THE TIME* of that assignment. (Don't ask me why! Shouldn't be true, but...)

So since my code above seemed to work, it seems to me the answer is to just move that assignment inside the function:

But... but with this new code, I don't see any reason to refer to that variable, anyway.

What about:
Code:
    birdSound.addEventListener(
        'timeupdate', 
        function () {
            console.log(this.currentTime);
            if (coverBirds.paused) 
           {
                if (this.currentTime > trigger ) 
                {
                    alert("eventalert");
                    birdSound.play; // **** IS THIS RIGHT ??? ****
                }
           }
       }
    );
But in that code, shouldn't birdSound.play really be coverBirds.play ?

And are you sure it should be .play? You used that property, above, to indicate that the element *is* playing (or not). Here you seem to be wanting to invoke a *method* that tells it to start playing?

I just checked the docs. I'm right. To start something playing you use .play( ) -- call the method. Using just .play doesn't tell you anything except whether the object in question *has* a play method! So you were using it wrong all along.

So I *think* the line in question should be coverBirds.play( )

Assuming I understand your intent.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.

Last edited by Old Pedant; 02-14-2013 at 01:14 AM..
Old Pedant is offline   Reply With Quote
Old 02-14-2013, 08:52 AM   PM User | #7
Entity_
New Coder

 
Join Date: Dec 2012
Location: Ingerlund
Posts: 62
Thanks: 8
Thanked 4 Times in 4 Posts
Entity_ is an unknown quantity at this point
Code:
    var birdSound = document.getElementById("player");
    var coverBirds = document.getElementById("coverBirds");
    var trigger = 5;
    
    

    birdSound.addEventListener(
       'timeupdate',
       function () {
           console.log(this.currentTime);
           if (coverBirds.paused) {
               if (this.currentTime > trigger) {
                   console.log("eventalert");
                   coverBirds.play(); 
               }
           }
       }
   );
Sorry for not making it clearer before. I quickly typed in the birdSound.play line you found because I had I hadn't actually got round to calling the new sound first. I wanted to first make sure it was working, and it still isn't. Here's where I'm at now. This whole code works fine if you change the conditional

Code:
 if (this.currentTime > trigger)
to

Code:
 if (this.currentTime < trigger)

But if I keep it as > it just does nothing. While you are indeed astute for noticing the birdSound problem, that's something I typed in while writing the forum post just to make it clearer, but I accidentally typed the wrong variable and got the syntax wrong, so it's not the main problem with the code. I'm sure the issue is somewhere with the > as it works if you tell it to go <, and I've commented out successive lines until only this line remained.
Entity_ is offline   Reply With Quote
Old 02-14-2013, 10:45 AM   PM User | #8
Entity_
New Coder

 
Join Date: Dec 2012
Location: Ingerlund
Posts: 62
Thanks: 8
Thanked 4 Times in 4 Posts
Entity_ is an unknown quantity at this point
nevermind, copying the variable assignment inside the function fixed everything, issue closed. Thanks pedant.
Entity_ is offline   Reply With Quote
Old 02-14-2013, 08:35 PM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,249
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
Originally Posted by Entity_ View Post
nevermind, copying the variable assignment inside the function fixed everything
That is *SO* weird. As I said, that was just a hunch of mine. I don't pretend to see why it matters, but if it does, it does.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
Entity_ (02-15-2013)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:57 AM.


Advertisement
Log in to turn off these ads.