Playing Audio with Javascript
Copy link to clipboard
Copied
OK - I am sort of piggybacking off of this thread
Selecting and playing audio with javascript
In my case, I only have a single audio file that I would like to play on the click of a button.
Now... I know I can do this easily with an on success action and it works perfectly.
I also know that I can play the audio with an advanced action and it works fine.
But... I can do several other simple actions on a button with a single line of javascript.
cp.show
cp.hide
cp.disable
cp.enable
cp.changeState
window.parent.cpAPIInterface.gotoSlide
var=(++var)
var=(--var)
just to name a few...
I prefer to use these whenever I can because it is much faster to type or copy/paste longer strings of stuff than doing all the clicks in the GUI
So to my question...
Is there a short and sweet thing for playing an audio file you have in the library as well?
You know - something like
cp.play("sound.mp3");
cp.playAudio("sound.mp3");
window.parent.cpAPIInterface.play("sound.mp3");
I have tried many other combinations of things similar to the above as well with no luck. The thread referenced above was the only thing that really touched it that I came across. I have also tried these with the audio loaded to the slide as a background to see if it would play again
Something that can run under Execute Javascript vs HTML Animation?
What am I missing? I am clearly not hitting the right syntax. Or is there really that much more going on behind the 'On Success - Play Audio'?
Copy link to clipboard
Copied
cp.playAudio(‘filenameWithoutSuffix’)
cp.stopAudio(‘filenameWithoutSuffix’)
You would need to load the .mp3 file into the /ar folder.
Copy link to clipboard
Copied
Is this really working, Jeremy? Sounds too good to be true. Anyway, will test it out later. Do you import the sound file to Cp? Meaning, that it is in the Library? Or do you add such a sound file after publishing to the /ar folder?
Klaus
Copy link to clipboard
Copied
I didn' see Jeremy around since a while. But he clearly states that you have to put the audio clips in the /ar-subfolder of the published folder. That happens automatically for audio clips which are started from a command 'Play Audio' within CP, but apparently not when it is done with JS.
Copy link to clipboard
Copied
Were you able to get this code to work? I've tried the cp.playAudio shown below, but with no success. Thanks.
Copy link to clipboard
Copied
Nothing is visible below.
Copy link to clipboard
Copied
The play audio code in Jeremy_Shimmerman's post
Copy link to clipboard
Copied
He forgot to add the ; at the end. How did you test? Since you need to add the audio file to the ar subfolder in the published folder, you'll need to upload to a webserver for testing.
Any reasons why you don't use the command Play Audio in Captivate?
Copy link to clipboard
Copied
Undoubtedly you've moved on, but hopefully this can help somebody with a similar problem (like me) in the future:
1) Create an object and name it whatever you want (maybe "SoundHolder_01")
2) Select the object and under Properties>Options, select "Add Audio" and choose the sound you want to play
3) Make the object invisible (0% opacity is easiest) and then hide the object (the dots in the eyeball column of the timeline)
4) Write your script, and where you want to play the audio, use the following:
cp.show("SoundHolder_01"); //Reveals the object on the stage that has the audio attached to it, which causes it to play
setTimeout(function(){cp.hide("SoundHolder_01")}, 500); //Waits long enough for the audio to play, then hides its holder object again, effectively "resetting" it – curently 500 miliseconds (.5 seconds)
Copy link to clipboard
Copied
I call that type of objects Audio Objects, have been describing them since many years. However, be careful: may not work for responsive projects, is listed as unsupported.
Copy link to clipboard
Copied
The audio-objects method works in responsive projects just as well.
(Cp 11.5.5.675 MacOS 10.15.7)
Copy link to clipboard
Copied
Thanks for sharing this idea.
What I have come to learn since posting this question originally is that Captivate conveniently renames audio files brought into the library for reasons I do not know but it is nonetheless frustrating to give something a name and think you know what you are trying to reference but... umm... no.
When I work with my audio now - I basically take one of three approaches:
1. Make two buttons and use a simple action of Play Audio on a speaker button and a Stop Triggered Audio on a mute button.
2. Make an icon with two states for sound on/sound off and a click box with advanced action that will change the state, play the audio, and then execute JavaScript to perform the setTimeout and change the state back.
3. Make an invisible button with a simple Play Audio action and place it somewhere discrete on the stage and use JavaScript to "click" that button when I need it.
I prefer to avoid after-publish modifications when possible.
I would prefer to simply call the name of my audio file but it is what it is - I suppose.

