Skip to main content
Stagprime2687219
Legend
March 30, 2018
Question

Playing Audio with Javascript

  • March 30, 2018
  • 3 replies
  • 4175 views

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'?

This topic has been closed for replies.

3 replies

Participant
June 24, 2020

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)

Lilybiri
Legend
June 24, 2020

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.

kdmemory
Inspiring
April 16, 2023

The audio-objects method works in responsive projects just as well.

(Cp 11.5.5.675 MacOS 10.15.7)

Known Participant
December 5, 2019

Were you able to get this code to work? I've tried the cp.playAudio shown below, but with no success. Thanks.

Lilybiri
Legend
December 5, 2019

Nothing is visible below.

Known Participant
December 5, 2019

The play audio code in Jeremy_Shimmerman's post

 
Jeremy Shimmerman
Participating Frequently
April 17, 2018

cp.playAudio(‘filenameWithoutSuffix’)

cp.stopAudio(‘filenameWithoutSuffix’)

You would need to load the .mp3 file into the /ar folder. 

kdmemory
Inspiring
June 26, 2020

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

Lilybiri
Legend
June 26, 2020

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.