currentTime of player never equals duration of player

Visuals, audio, animation. Blended, not stirred. If LiveCode is part of your rich media production toolbox, this is the forum for you.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
bbalmerTotalFluency
Posts: 46
Joined: Mon Apr 06, 2020 1:19 pm
Location: Thailand
Contact:

currentTime of player never equals duration of player

Post by bbalmerTotalFluency » Thu Jan 18, 2024 2:43 pm

Hello:

I'm using a player object on macOS to play some audio. I want to do something once the file has stopped playing. I have been advised that some code like this will do the trick -

if the currentTime of player "audio" >= the duration of player "audio" then .... do whatever. Makes sense

However, the maximum currentTime I get back for one of my files is 762 milliseconds and the duration of the player is 938

I've temporarily "solved" my problem by exiting when the currentTime is 200 less than the duration of the player. We all know that is an ugly work-around.

Is it possible that the player stops playing when the sound stops i.e my files are of longer duration than the sound they contain? I know there is a short amount of silence before and after the audio.

I just had a file with a duration of 1536 and a max currentTime of 800. That fails my workaround. I have a second workaround of a waiting a max of 2 seconds but that is just getting ugly.
Last edited by bbalmerTotalFluency on Thu Jan 18, 2024 3:01 pm, edited 1 time in total.

stam
Posts: 2550
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: currentTime of player never equals duration of player

Post by stam » Thu Jan 18, 2024 2:54 pm

I might have misunderstood your question - but aren't you looking for the playStopped message? This is a message sent to a player when it stops playing.

eg

Code: Select all

on playStopped 
  hide the target
  show button "Start Playing"
end playStopped

bbalmerTotalFluency
Posts: 46
Joined: Mon Apr 06, 2020 1:19 pm
Location: Thailand
Contact:

Re: currentTime of player never equals duration of player

Post by bbalmerTotalFluency » Thu Jan 18, 2024 3:03 pm

You have not misunderstood my question. I was just taking advice on a way to solve this problem using currentTime and Duration (which makes logical sense to me).

I guess I will have to use the solution you propose (not reluctantly, gratefully), but I'd like to understand first why my currentTime never reaches my Duration since that is popular advice and appears to be prone to error.

I've used your solution and it works like a charm - thank you. I'd just still like to know the meaning / significance / cause of my stated problem.

P.S. I don't want anyone to take this as a complaint - just a suggestion because I love livecode and I get great service from these people. Just that if some advice doesn't work, wouldn't it be great if somehow such advice could be flagged as no longer working.

stam
Posts: 2550
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: currentTime of player never equals duration of player

Post by stam » Thu Jan 18, 2024 3:42 pm

bbalmerTotalFluency wrote:
Thu Jan 18, 2024 3:03 pm
You have not misunderstood my question. I was just taking advice on a way to solve this problem using currentTime and Duration (which makes logical sense to me).
Are you sure these numbers are different?

I put this in the script of a player with a random .wav file:

Code: Select all

on playStopped
    put the currentTime of me & return & the duration of me
end playStopped
I get two identical numbers.

I wonder if your issue is with how you're actually checking currentTime?

Your code hinges on

Code: Select all

if the currentTime of player "audio" >= the duration of player "audio" then .... do whatever. Makes sense
not sure that makes much sense to me though - where is the if statement being called from? Why do people never post their code...

The only sensible place to check currentTime that I can see would be in the currentTimeChanged handler of the player, eg:

Code: Select all

on currentTimeChanged theInterval
    put the currentTime of me & return & the duration of me
    if the currentTime of me = the duration of me then put return & "END!" after msg
end currentTimeChanged
This again works exactly as expected (i.e. the final currentTime = duration)... and I fail to see how this is in any way better than just coding the playStopped message if I'm honest.

You could check the currentTime of the player from elsewhere, outside of the player's script, but you'd have to poll the player every, or every few, milliseconds which seems pointlessly wasteful...

stam
Posts: 2550
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: currentTime of player never equals duration of player

Post by stam » Thu Jan 18, 2024 4:31 pm

stam wrote:
Thu Jan 18, 2024 3:42 pm
The only sensible place to check currentTime that I can see would be in the currentTimeChanged handler of the player
I take that back. I put this in the card script and it worked a treat:

Code: Select all

on mouseUp
    start player "test"
    repeat while the currentTime of player "test" <= the duration of player "test"
    end repeat
    put the currentTime of player "test" & return & the duration of player "test" & return  & "DONE AND BEEP"
    beep
end mouseUp
Again, the final currentTime = the duration of the player and subsequent actions happened on cue.

So not sure where your issue is if I'm honest - perhaps post some of your actual code?

Stam

jiml
Posts: 336
Joined: Sat Dec 09, 2006 1:27 am
Location: Los Angeles

Re: currentTime of player never equals duration of player

Post by jiml » Fri Jan 19, 2024 9:06 pm

the maximum currentTime I get back for one of my files is 762 milliseconds and the duration of the player is 938
Check out timescale in the dictionary:
The timeScale is the number of intervals per second of a movie or sound. These intervals are used for the player's startTime, endTime, duration, and currentTime properties, and you can use the timeScale property to convert from the time scale used by the movie or sound to seconds.
Perhaps the timescale of your movie is 1.230971. That is, 938/762.

Jim Lambert

jiml
Posts: 336
Joined: Sat Dec 09, 2006 1:27 am
Location: Los Angeles

Re: currentTime of player never equals duration of player

Post by jiml » Fri Jan 19, 2024 9:08 pm

Also 762 milliseconds is a very short duration. Are you certain about that?

stam
Posts: 2550
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: currentTime of player never equals duration of player

Post by stam » Fri Jan 19, 2024 9:35 pm

Well, easy enough to check… the OP can just mimic my code and what results are put in the msgBox. If they are 2 different numbers that would imply there are different timescales in play perhaps?

But keeping in mind the solution is actually just to use the playStopped handler as above - this use case is the very reason for its existence - this is more of an academic discussion ;)

jiml
Posts: 336
Joined: Sat Dec 09, 2006 1:27 am
Location: Los Angeles

Re: currentTime of player never equals duration of player

Post by jiml » Sat Jan 20, 2024 10:50 pm

Indeed playstopped is the way to accomplish his goal.
I proffered a possible solution to the title of this thread.

bbalmerTotalFluency
Posts: 46
Joined: Mon Apr 06, 2020 1:19 pm
Location: Thailand
Contact:

Re: currentTime of player never equals duration of player

Post by bbalmerTotalFluency » Sun Jan 21, 2024 1:20 pm

Thanks ALL.

In the end I see the elegance and simplicity of the original reply using the callbacks and now everything works properly, so I'm going to stick with that as the thing that feels like the most correct way to handle my problem.

The reason for the short duration is that it is just an audio file of someone saying one word.

Post Reply

Return to “Multimedia”