Audio Play / Pause switching Buton


#1

Hi there !

And thank you all for making these forums so alive and helpful !

Here is my question :

I have a music playing with my main timeline. Then I want to have a button that makes music play / pause (switching function)

I found this Java functions but I can’t make it work. First, I don’t really know how to encode an “unique ID” for a sound as it is easy to do with any other elements.

(hypeDocument.getElementById(‘myAudio’).paused ? hypeDocument.getElementById(‘myAudio’).play() : hypeDocument.getElementById(‘myAudio’).pause());

Could you help me to achieve this ?


#2

How are you adding your sound? Timeline action? Dropping onto scene area? In HTML inside an element?

If you are dropping onto the scene area then you cannot manipulate it using Javascript (btw javascript is different to Java try not to mix them) because you cannot reference the audio with an id. You need to be able to access the <audio> element.

If you are calling it using a timeline then again for the reasons stated above you cannot access in the same way.

You can access it however, if you are using code (<audio> tag inside an element with an id=‘myAudio’) inside a rectangle or other element. This way you can use your shorthand javascript conditional statement.

(hypeDocument.getElementById('myAudio').paused) ? hypeDocument.getElementById('myAudio').play(); : hypeDocument.getElementById('myAudio').pause();

Another option is to create a new audio element on scene load using the following javascript.

window.audio = new Audio('${resourcesFolderName}/name-ofyour-file.mp3');

now you can use the following javascript with a button on mouse click.

(audio.paused) ? audio.play() : audio.pause();

Note window.audio makes the variable belong to the browser window and can be called at anytime.


(laggarto) #3

Thanks @DBear, flawless victory using the javascript