Callbacks

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.

Attached Files

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:

intervalNumber, messageName

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

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 and

Our resources

Our resources

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.

on mouseUp

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

on loadSoundFile pSoundFile

set the filename of player "story" to pSoundFile

end loadSoundFile

 

Using Callbacks

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 a variable that has been populated with the text from the chosen file by

on loadTextFile pTextFile

put url ("file:" & pTextFile) into sStoryText

end loadTextFile

Calling back

Calling back

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

3473,showText 1

7171,showText 2

10527,showText 3

13232,showText 4

17150,showText 5

20178,showText 6

22599,showText 7

24800,showText 8

27123,showText 9

28758,showText 10

31300,showText 11

32681,showText 12

35341,showText 13

37900,showText 14

40700,showText 15

43728,showText 16

46866,showText 17

48700,showText 18

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.

Further development

Further development

To make our story book more appealing we can add images and graphics as well.

0 Comments

Add your comment

E-Mail me when someone replies to this comment