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.
You can download the stack from this lesson here: https://tinyurl.com/y9x2qbfo
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
For a simple talking story book first we need is a new stack with a field called "words" to display our text (1) and a player called "story" (2) to play our sound file.
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.
Add this code to the "Select Sound File" button
on mouseUp local tSoundFile 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 loadSoundFile tSoundFile end if end mouseUp
Add this code to the "Select Text File" button
on mouseUp local tTextFile answer file "Please choose a text file" if the result is not "cancel" then // The user has not cancelled the dialog put it into tTextFile loadTextFile tTextFile end if end mouseUp
Add the loadSoundFile and loadTextFile handlers to the Card Script.
local sStoryText on loadSoundFile pSoundFile set the filename of player "story" to pSoundFile end loadSoundFile on loadTextFile pTextFile put url ("file:" & pTextFile) into sStoryText end loadTextFile
Remember to declare a script local variable to hold the story text.
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" end showText
We can call this handler with a line number to have that line of text displayed, sStoryText is the variable we populated from the text file earlier.
Add the showText handler to the Card Script.
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.
This is the list of callbacks for this example
20,showText 1 3473,showText 2 7171,showText 3 10527,showText 4 13232,showText 5 17150,showText 6 20178,showText 7 22599,showText 8 24800,showText 9 27123,showText 10 28758,showText 11 31300,showText 12 32681,showText 13 35341,showText 14 37900,showText 15 40700,showText 16 43728,showText 17 46866,showText 18 48700,showText 19
All we need to do is set the callbacks property of the player to the list of callbacks.
Add a button Select Callbacks File and add this code to it
on mouseUp local tCallbacksFile answer file "Please choose a callbacks file" if the result is not "cancel" then // The user has not cancelled the dialog put it into tCallbacksFile loadCallbacks tCallbacksFile end if end mouseUp
And then add the loadCallbacks handler to the card script
on loadCallbacks pFile local tCallbacks put url ("file:" & pFile) into tCallbacks set the callbacks of player "story" to tCallbacks end loadCallbacks
Then select the callbacks file and start the player and the text will appear along with the audio file.
To make our story book more appealing we can add images and graphics as well.