Looping video and callbacks - guidance anyone?

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
Howlernator
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 18
Joined: Wed Oct 13, 2010 4:44 am

Looping video and callbacks - guidance anyone?

Post by Howlernator » Wed Jan 26, 2011 10:30 pm

Heya Folks,

I have the feeling that this is an old chestnut, but thought I would check...

Just tried to implement some callbacks based logic from a player object playing a LOOPING video.

Seems that the callbacks are registered OK for the first run, but cannot hear boo from them after that inside a looping playback of video using the 'Loop' property for the player object.

Any pointers? Can i construct some sort of timer in the event that either player objects (or perhaps QTExternal, which I haven't checked with yet) are not up to this?

Card Script code :

Code: Select all

on swapMovie tMovieIndex
    lock screen
    put the cPrefs["cCurrentVideoFilePath"] of this stack & "car_" & tMovieIndex & ".mov" into tSwappedMovie
    set the filename of player "CallResponseWindow" to tSwappedMovie
    set the currentTime of player "CallResponseWindow" to zero
    visual effect dissolve very fast
    unlock screen
    put the duration of player "CallResponseWindow" into tDuration
    set the callbacks of player "CallResponseWindow" to 150,showYourTurnOff & cr & (tDuration / 2) & ",showYourTurn"
end swapMovie
    
... and the callback handlers:

Code: Select all

on showYourTurn
    set the visible of grp "YourTurn" to not the visible of grp "YourTurn"
end showYourTurn

on showYourTurnOff
    set the visible of grp "YourTurn" to false
end showYourTurnOff
Basically, the video plays, the callback gets through for the first 'showYourTurn' halfway through the first movie and switches the group visible on, then it just stays on, when it should switch off at the start of the next loop... at duration = 150.

.... am i missing something about durations and looping objects perhaps?

Guidance appreciated!

Cheers,

A.

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Looping video and callbacks - guidance anyone?

Post by Klaus » Wed Jan 26, 2011 11:59 pm

Hi A.,

just a shot in the dark:
Sometimes it helps when you set the filename of a player to empty to "reset"
the player object before setting it to another movie file:
...
lock screen
set the filename of player "CallResponseWindow" to EMPTY
put the cPrefs["cCurrentVideoFilePath"] of this stack & "car_" & tMovieIndex & ".mov" into tSwappedMovie
set the filename of player "CallResponseWindow" to tSwappedMovie
...

At least worth a try :)

AND put the callbacks in QUOTES just to be sure!

EDIT! There was one QUOTE too much!
...
###WRONG: set the callbacks of player "CallResponseWindow" to "150,showYourTurnOff" & cr & QUOTE & (tDuration / 2) & QUOTE & ",showYourTurn"
set the callbacks of player "CallResponseWindow" to "150,showYourTurnOff" & cr & QUOTE & (tDuration / 2) & ",showYourTurn"
,,,


Best

Klaus

P.S.
The Enhanced QT External does not help in this case!

jiml
Posts: 339
Joined: Sat Dec 09, 2006 1:27 am

Re: Looping video and callbacks - guidance anyone?

Post by jiml » Thu Jan 27, 2011 6:24 am

If worst comes to worst, you could just roll your own triggers.
Either by setting up timers with "send in time" as you mentioned.
Or, keep checking the currentTime and compare that against a list of trigger times.

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Looping video and callbacks - guidance anyone?

Post by Klaus » Thu Jan 27, 2011 11:57 am

Hi Jim,

you are correct, but "callbacks" DO work in LiveCode :wink:


Best

Klaus

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: Looping video and callbacks - guidance anyone?

Post by bn » Thu Jan 27, 2011 1:22 pm

Hi Howlernator,

I tried and did not get the callbacks to work when the movie's looping is true, as you describe.

An easy way out of this is to set the looping to false but append in the callbacks one last callback at the end of the movie = the duration of the movie which triggers a restart of the movie. Thus you loop the movie and your callbacks work.

in the script of the player:

Code: Select all

on showYourTurnOff
   hide btn "test"
end showYourTurnOff

on showYourTurn
   show btn "test"
end showYourTurn

on restartPlayer
   start player "CallResponseWindow"
end restartPlayer
When setting up the callbacks:

Code: Select all

on mouseUp
   put the duration of player "CallResponseWindow" into tDuration
   put (tDuration / 4),"showYourTurnOff" & cr &  (tDuration / 2) & ",showYourTurn" & cr & tDuration & ",restartPlayer" into tCall
   set the callbacks of player "CallResponseWindow" to tCall
end mouseUp
tested and works for me.

Kind regards

Bernd

Howlernator
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 18
Joined: Wed Oct 13, 2010 4:44 am

Re: Looping video and callbacks - guidance anyone?

Post by Howlernator » Thu Feb 10, 2011 2:59 pm

Still working on this one guys.

I've tried Klaus's suggestion and am getting the same result. Callbacks work OK on first playthrough, but not subsequent ones.

Bernds solution of a manual restart to create the loop playback works reasonably well, but it still really does seem hit and miss with the callback messages actually getting through. Sometimes they fire, sometimes they don't. I tried a simple beep test on this for each of the embedded commands, and got really intermittent results. In addition to restart using this manual method creates an undesirable gap in the looped audio, which detracts significantly from the experience.

No real consistency each time each command was supposed to fire.

The player object DOES report an accurate, 'looping' currentTime property during looped playback, so at least that part is working. I ran this test from a separate button triggering a 'put the currentTime' script, and tested manually during playback.

Basically, it's almost like any commands issued by callback cannot run AGAIN until the start command is issued to the player object.

I've not implemented a timer before with 'send in time' or the 'wait' command... but I'm thinking this is the only option now...unless there is something really simple to get the callback messages through during a loop playback.

As I understand it, I would have to:

1. calculate movie length based on loaded video file
2. inside the 'start player' script, add additional timer script which :
- keeps running while ever playback is running
- sends a show/hide command at points 0, halfway, and tDuration (or other desired points)
- stop running when player object is stopped

QUESTIONS:
1. how do you convert the duration value of a player which is in 'ticks' (or something?) into a milliseconds reference for the timer script?
2. any quick pointers on the code for the timer in item 2, and any gotchyas that might be there?

Thanks guys.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: Looping video and callbacks - guidance anyone?

Post by bn » Thu Feb 10, 2011 4:26 pm

Hi Howlernator,
1. how do you convert the duration value of a player which is in 'ticks' (or something?) into a milliseconds reference for the timer script?
The duration is a little more complicated.
a movie or audio file in the player has a timescale. The timescale is usually 600 for movies, which means each second is 'sliced' 600 times. The timescale is set when the movie is made. It is nothing livecode does.
The duration is the the timescale times the seconds (plus fractions thereof) the movie plays.

Example
take a movie that plays for 3.5 seconds. The timescale of the movie is 600. This is a duration of 600 * 3.5 = 2100.
take a movie that plays for 3.5 seconds. The timescale of that movie is 100. This is a duration of 100*3.5 = 350.
So your millisecond timer would have to do the calculation depending on the timescale.

Bernds solution of a manual restart to create the loop playback works reasonably well, but it still really does seem hit and miss with the callback messages actually getting through. Sometimes they fire, sometimes they don't. I tried a simple beep test on this for each of the embedded commands, and got really intermittent results. In addition to restart using this manual method creates an undesirable gap in the looped audio, which detracts significantly from the experience.
Well I tested again and never got dropped callbacks. And the sound is just as it would be with the looping. The question is why do you have dropped callbacks? This could be if you have blocking code in triggered by your callbacks. You might want to explain what the code looks like your callbacks trigger. If it is the code it would help if you could post an example of what you do.

I attach a little stack I did my testing on. This is with the technique when the currentTime reaches the duration it triggers a callback that restarts the player.
I did not come up with another technique. Though instead of a callback you could use the playStopped message that is send to the player when the movie stops/reaches the end.(also in the stack). Of course the callback only trigger the hiding or showing of your test button and a beep.

Kind regards

Bernd


Kind regards
Attachments
loopPlayerCallback.livecode.zip
(1.86 KiB) Downloaded 463 times

Howlernator
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 18
Joined: Wed Oct 13, 2010 4:44 am

Re: Looping video and callbacks - guidance anyone?

Post by Howlernator » Fri Feb 11, 2011 4:22 am

Bernd,

Thank you.

I really think the issue may be something to do with an LC memory management or caching issue during playback which inadvertently blocks or suspends callbacks. I loaded a number of different movies (.movs, but i note that yours was a referenced .avi file) into your test stack and was able to reproduce the same behaviour. Basically, sometimes the callbacks fire, sometimes they don't. I didn't test this outside the IDE in runtime, but this is a feature that has to be pretty robust.

I've got the solution working using this timer, triggered during after the 'start player' command:

Load the movie and calc duration into milliseconds to a script local variable:

Code: Select all

on swapMovie tMovieIndex
    lock screen
    set the filename of player "CallResponseWindow" to EMPTY
    set the cPlaying of this card to empty
    set the visible of grp "YourTurn" to false
    put the cPrefs["cCurrentVideoFilePath"] of this stack & "car_" & tMovieIndex & ".mov" into tSwappedMovie
    set the filename of player "CallResponseWindow" to tSwappedMovie
    set the currentTime of player "CallResponseWindow" to zero
    visual effect dissolve very fast
    unlock screen
    put (the duration of player "CallResponseWindow" / the timeScale of player "CallResponseWindow")*1000  into tDuration
end swapMovie
run the 'play' sequence:

Code: Select all

on changePlayState
    if the cPlaying of this card is empty then
        set the currentTime of player "CallResponseWindow" to 0
        start player "CallResponseWindow"
        set the cPlaying of this card to 1
        set the hilite of btn "Play" to true
        runYourTurnTimer
    else
        stop player "CallResponseWindow"
        set the cPlaying of this card to empty
        set the currentTime of player "CallResponseWindow" to 0
        set the hilite of btn "Play" to false
        set the visible of grp "YourTurn" to false
    end if
end changePlayState
Then to do the timed event inside the movie:

Code: Select all

on runYourTurnTimer
    put round(tDuration/2) into tAlertTime
    put tAlertTime
    repeat while the cPlaying of this card = 1
        wait tAlertTime milliseconds with messages
        if the cPlaying of this card =1 then
            set the visible of grp "YourTurn" to not the visible of grp "YourTurn"
        end if
    end repeat
end runYourTurnTimer

... it could have been sooooo elegant with callbacks, but alas. It's working pretty snappily using the above anyway. Hope that helps those who come after me here!

Final question before I bug y'all no more on this thread! :
I've found out (hopefully mistakenly) that you cannot load an AUDIO file into a player object? I've tried loading as .wav, renaming the .wav to .mov. No response either from message window or scripted cmds. Is that correct? Can we only load .mov files with a VIDEO track into a player object? That would make me a little sad :( Hopefully I'm missing something as I was counting on being able to use some of the player commands to control multitrack audio elsewhere.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: Looping video and callbacks - guidance anyone?

Post by bn » Fri Feb 11, 2011 1:11 pm

Hi Holernator,

I dont know what is going on. But I am a little surprised since I use the callbacks in one of my programs extensively to get a frame triggered response from short movie clips. This program works for years and we never had problems with it.
Those are short clips with about a thousand frames and a thousand callbacks. The callbacks trigger the recording of the mouse coordinates. The program checks the amount of recorded coordinates against the number of callbacks and throws an error if recorded coordinates and callbacks are not the same. Since this works without problem I was pretty confident that you would get this to work.

One thing that I noticed: you set the hilite of a button to true. If this is on a Mac and it is the standard blue pulsating button then this takes an great amount of processing time. The implementation of the pulsatingdefault button in livecode is very inefficient. Depending on the hardware it uses 20 to 40 percent of the processor time. Just for pulsating. If you are not using the blue Mac default blue pulsating button this is not an issue. Setting the hilite of any other button is ok.
Final question before I bug y'all no more on this thread! :
I've found out (hopefully mistakenly) that you cannot load an AUDIO file into a player object? I've tried loading as .wav, renaming the .wav to .mov. No response either from message window or scripted cmds. Is that correct? Can we only load .mov files with a VIDEO track into a player object? That would make me a little sad Hopefully I'm missing something as I was counting on being able to use some of the player commands to control multitrack audio elsewhere.
You can very well play audio files in a player object. It plays all the files that your current quicktime supports. AVI and MOV are just containers that contain the media in different compression formats. These are determined by the codecs. That means if you have audio/movie files that Quickime does not support natively you can extend the playable formats by installing codecs. One way to play many WMV formats on a mac is to install flipforMac. Or the free Perian.

The same applies to Windows Media Player. If you want to extend the formats of WMP you have to install the codecs.

But changing the file extension from avi to mov does not help.
What might have gotten you is that Livecode defaults to mov files in the dialog to choose files to set the filename of a player. You have the option to set it to "all files". Then it lets you choose any file. You have to find out whether it plays in Quicktime or not. (see codecs above)

Sorry to be of little help here. If you want me to look into this any further you would have to upload your zipped example stack that shows the dropping of callbacks. And tell us what operating system, what livecode version and what hardware your are using.

Kind regards

Bernd

Howlernator
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 18
Joined: Wed Oct 13, 2010 4:44 am

Re: Looping video and callbacks - guidance anyone?

Post by Howlernator » Fri Feb 11, 2011 2:33 pm

Hiya Bernd,

Well, I would love to get to the bottom of this if possible. Please find attached a copy of your stack (slightly revised with some explanatory button names, but no script changes) AND the movie file in question here that i'm getting the unstable performance with on callbacks.

I'm running :
Mac OSX 10.6.6
LC 4.5.2 with a few of the usual IDE plugins etc.. but nothing special.

When I run these files here, I get either:

1. Playback of one time through the movie, and no further playback
2. Up to FOUR (but no more) repeated loops where the callbacks seem to all work for a while, then not.
3. Some intermittent, random combination of the above.

I'd be keen to hear how it runs at your end...

Regards,

A.
Attachments
LoopPlayerCallbackv2.zip
(90.8 KiB) Downloaded 451 times

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: Looping video and callbacks - guidance anyone?

Post by bn » Fri Feb 11, 2011 3:37 pm

Hi Howlernator,

I finally saw what you see. At times the callbacks do not work and the movie stops.
This was after trying the stack in different versions of Livecode and saving in between. Then all of a sudden your problem appeared. I then set the filename to "" and set it to your file again and it worked stably after. I don't know what causes the problem and I have not been able to reproduce it by opening the stack in older versions, saving and then reopening in newer versions of livecode. It was again stable for me for many rounds of Oh-la Oh-le... :)

So this is not good news, especially since it is not reproducible for me. No recipe, no cure...

Could you try to reset the filename of your player and see if it honores the callbacks afterwards?
Maybe you can come up with a recipe.

This is all weird. The only general hunch I have is that apple is changing the old implementation of quicktime as of 10.6
Livcode uses the old application programming interface API (carbon) which is not well supported in 10.6 anymore.
Livecode is aware of the problem and ponders what to do with its implementation of Quicktime. This problem also shows in the video preview for the built-in iSight cameras.

What I do hear clearly now is the short lag in the soundtrack when the movie is not looping but restarted at the end of the movie. I am afraid there is no work around for that.

Kind regards

Bernd

Post Reply