Audio Play Error with Javascript


(Matt) #1

Hi -
For some reason this seemingly very straightforward javascript is not working:

var myAudio = document.getElementById("audio1"); 
          myAudio.play();

I made a test project that has it begin called from within another javascript function because that is how I need it to be called.

The error I’m getting is: TypeError: myAudio.play is not a functionaudioTest.zip (82.1 KB)


(Mark Hunte) #2

If you want that to work you need to create you own audio conatiner and set the audio scr to your file in the resource.
There is plent about this on site… :smile:


#3

@matt5834

Hi Matt!

Try this: audioTest-JHSv1.hype.zip (79.1 KB)

There is now just one button “Play Audio” and clicking it triggers the function “audioTest()”. The code is identical to what You previously had written.

What is different as @MarkHunte indicated is we are using the “Play Audio” button (or any basically any element - could be simply a rectangle) as a container to place the “audio tag”.

"Play Audio" button innerHTML:

Play Audio // This is the visible text

<audio id="audio1" width="400" preload="auto">
	<source src="${resourcesFolderName}/audio1.m4a" type="audio/mpeg">
</audio>

Important Note: The id referenced by “audioTest()” is the id in the audio tag itself - not the id of the container (in this case the button) as referenced in the Hype interface settings in the “Identity Inspector”.


For more info check out this post by @Daniel:

(Matt) #4

Thanks @JimScott and @MarkHunte

I still can’t get the audio to play when it is done via a javascript call. For example, in your example Jim the audio does not play onSceneLoad.

Sorry if I’m missing something basic…

-Matt


#5

Hi Matt!

I think You are running into a browser setting issue.

On my iMac to get the sound file to play “On Scene Load” In the Hype preview using Safari I had to select:

“Safari” menu > “Settings for this website…” > "Auto-play: “Allow All Auto-Play”

08%20PM