currentTime of player never equals duration of player
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- Posts: 84
- Joined: Mon Apr 06, 2020 1:19 pm
- Contact:
currentTime of player never equals duration of player
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.
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.
Re: currentTime of player never equals duration of player
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
eg
Code: Select all
on playStopped
hide the target
show button "Start Playing"
end playStopped
-
- Posts: 84
- Joined: Mon Apr 06, 2020 1:19 pm
- Contact:
Re: currentTime of player never equals duration of player
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.
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.
Re: currentTime of player never equals duration of player
Are you sure these numbers are different?bbalmerTotalFluency wrote: ↑Thu Jan 18, 2024 3:03 pmYou 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 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 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
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
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...
Re: currentTime of player never equals duration of 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
So not sure where your issue is if I'm honest - perhaps post some of your actual code?
Stam
Re: currentTime of player never equals duration of player
Check out timescale in the dictionary:the maximum currentTime I get back for one of my files is 762 milliseconds and the duration of the player is 938
Perhaps the timescale of your movie is 1.230971. That is, 938/762.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.
Jim Lambert
Re: currentTime of player never equals duration of player
Also 762 milliseconds is a very short duration. Are you certain about that?
Re: currentTime of player never equals duration of player
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
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
Re: currentTime of player never equals duration of player
Indeed playstopped is the way to accomplish his goal.
I proffered a possible solution to the title of this thread.
I proffered a possible solution to the title of this thread.
-
- Posts: 84
- Joined: Mon Apr 06, 2020 1:19 pm
- Contact:
Re: currentTime of player never equals duration of player
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.
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.