Load sound; create simple play/pause button

Actions you can use in your projects.

Load sound; create simple play/pause button

Postby Clifton » Fri Jul 10, 2015 5:06 pm

The PowerPac provides full audio loading via the function pgSoundLoader().
The PowerPac provides full audio management and control via the function pgSoundControl().

Example of loading a sound in a paused state:
  1. Create a folder in the same folder as your tbk called mp3.
  2. Copy at least one sound mp3 file into the folder.
  3. Add the folder to your PowerPac-enabled book as shown below.

      Image 2.png
      Add a mp3 sound folder to the tbk export.
      Image 2.png (97.15 KiB) Viewed 4760 times
  4. Add the following action to the on load page event of your page. You will need to change your audio url to match the mp3 you want to play.

      Image 1.png
      On load page event action
      Image 1.png (24.66 KiB) Viewed 4760 times

      Notice that we have given our sound a name attribute called "Narration_1". This unique string is used to control and manage the sound. The string can be any string you want to use to refer to this audio playback stream. You can even load two sounds that point to the same mp3 file and use different names. The sound engine treats them as two different audio streams and allows you to manage them independently of each other even though they are streaming from the same source.

      Also when the sound is loaded, we told the PowerPac sound engine to load the sound in a paused state.
  5. Draw a button to handle your play/pause action and add the following actions to it.

      Image 3.png
      On click action call to pgSoundControl()
      Image 3.png (28.21 KiB) Viewed 4760 times

      Notice that when the button is clicked, the sound engine will toggle the paused state of our audio "Narration_1".

      Now we need to know what state the audio is in when the button is clicked, so we add the next action and store the result in a variable. We called is isPaused but you can use any variable you want.

      Image 4.png
      On click action 2nd call to get audio state
      Image 4.png (28.31 KiB) Viewed 4760 times

      Finally, add the if/then actions to evaluate the variable isPaused. If it is true, the audio is paused and we change the caption of the button accordingly, etc.
  6. Export your book and give this a try. Of course, there are many other sound management things you can do with these functions. But this gives you a little start into the world of predictable audio.

    NOTE: To make this work seamlessly, you would need to handle the "end of audio" event that the PowerPac sends so that the audio file can be reloaded and the button caption reset to a default value when the audio file has finished playing. The PowerPac Help API contains help on handling this audio event. You may also contact us for assistance if necessary.

This will work in all supported browsers and devices.
Attachments
playPauseButton_v115.zip
Download souce file here (ToolBook v11.5)
(132.11 KiB) Downloaded 423 times
Clifton
Site Admin
 
Posts: 732
Joined: Tue Jan 14, 2014 1:04 am

Re: Load sound; create simple play/pause button

Postby Clifton » Thu Aug 06, 2015 4:14 pm

Here is another example showing a simple play/pause button AND alternately the use of a field to contain playback controls to control audio which is played in a different stream or channel.

THE URL:
http://www.pgsoftwaretools.com/powerpac/assessments/sound_mp3_examples

sound_mp3_examples.zip
TBK with audio mp3 (TB v10.5+ required)
(467.33 KiB) Downloaded 447 times
Clifton
Site Admin
 
Posts: 732
Joined: Tue Jan 14, 2014 1:04 am


Return to Action Editor Examples

Who is online

Users browsing this forum: No registered users and 1 guest

cron