Players are a great feature of Revolution, they can play audio or video allowing you to create rich media applications. Players can be stopped, paused, rewound - all the abilities you would expect of any media player - but one of the features that makes Revolution players really powerful is callbacks.
In this lesson we will see how you can use callbacks to make a fun and interesting application.
What is a callback?
Callbacks are a property of a player and are a list of entries, one per line, which allow you to trigger scripts at specific points during playback.
Each entry has the form:
The intervalNumber is a positive integer expressed in the internal player time format, this depends on the timescale of the player where the timescale is the number of intervals per second. In a player with a timescale of 500 interval 1000 would be 2 seconds into the movie or sound.
The message messageName is sent when the currentTime of the player reaches the given interval, allowing the user to synchronize other actions with the playback of a movie or sound file.
What can callbacks be used for?
One possible application of callbacks would be to create a talking storybook, a fun and educational program for children learning to read or a foreign language teaching tool. Callbacks can be used to change the graphics as the story is being read or show the text along with the voiceover.
To use callbacks to display text and graphics, you simply identify the intervals where you want them to change and set the callbacks of the player to send a message when it reaches those intervals.
Creating the Stack
Now we need the sound file we want to play and the text we want to display. I have a sound file of the first chapter of Alice in Wonderland being read aloud and the text of the first chapter stored in a text file. I have split the text file up into individual lines as we only want to show one line at a time.
Now we need a way to select our sound file and text file. We'll add 2 buttons to the stack Select Sound File(1) and Select Text File(2). These 2 button use the answer command to allow you to select the files you want to use. These in turn call handlers on the card that load in the files ready to be used.
answer file "Please choose a sound file"
if the result is not "cancel" then
## The user has not cancelled the dialog
put it into tSoundFile
on loadSoundFile pSoundFile
set the filename of player "story" to pSoundFile
Now we could just put the text into the field and start the player but that is not particularly interesting. What we will do instead is use callbacks to show one line of text at a time, along with the audio file.
To set up callbacks we need a handler that we can call when the player reaches a certain point, this handler will display the relevant text.
on showText pLine
put line pLine of sStoryText into field "words"
We can call this handler with a line number to have that line of text displayed, sStoryText is a variable that has been populated with the text from the chosen file by
on loadTextFile pTextFile
put url ("file:" & pTextFile) into sStoryText
The callbacks of a player are a list of commands to be called when the player reaches certain points. These points are specified by the interval or currentTime of the player. So in order for the talking storybook to work you need to know the points at which the showText handler should be called, I've done the hard work for this example...
This is the list of callbacks
All we need to do is
set the callbacks of player "story" to tCallbacks
Then just start the player and the text will appear along with the audio file.