• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Playing Audio with Javascript

Advisor ,
Mar 30, 2018 Mar 30, 2018

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

Views

2.7K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 17, 2018 Apr 17, 2018

Copy link to clipboard

Copied

cp.playAudio(‘filenameWithoutSuffix’)

cp.stopAudio(‘filenameWithoutSuffix’)

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jun 26, 2020 Jun 26, 2020

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 26, 2020 Jun 26, 2020

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 05, 2019 Dec 05, 2019

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 05, 2019 Dec 05, 2019

Copy link to clipboard

Copied

Nothing is visible below.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 05, 2019 Dec 05, 2019

Copy link to clipboard

Copied

The play audio code in Jeremy_Shimmerman's post

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 05, 2019 Dec 05, 2019

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 23, 2020 Jun 23, 2020

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)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 24, 2020 Jun 24, 2020

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 16, 2023 Apr 16, 2023

Copy link to clipboard

Copied

LATEST

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

(Cp 11.5.5.675 MacOS 10.15.7)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jun 24, 2020 Jun 24, 2020

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Help resources