0 votes
by (150 points)
var audio= document.createElement('audio');
audio.loop = true;

I tried using this in my javascript, but It doesn't work. I'm not sure what I'm doing wrong

1 Answer

0 votes
by (159k points)
selected by
Best answer

NOTE: I strongly suggest you use ChapelR's Harlowe Audio Lib add-on, it will save you from having to spend lots of time & effort tring to implement your own JavaScript based functionallity that supports all the inconsistancies between the different web-browsers and device types they run on.

I am going to assume that when you say "in my javascript" you mean within your project's Story Javascript area.

Due to security concerns many modern web-browsers now require that the end-user interacts with the web-page before it will allow media (like audio & video) to autoplay, and this secutiry restriction is likely the reason why the audio file isn't playing.

We generally recommend making the first Passage of your project some sort of welcome / summury / credits / etc screen that contains a link to a second passage that is the actual start of your story/game, and to play your audio autoplay code within this second passage. This way the end-user has to interact with the page at least once via the selection of that link, which will satisfy the security restriction.

I tested your code example by placing it with a <script> HTML element in such a second passage and it played.

This is the second passage of the project and the actual start of the story/game.
	var audio = document.createElement('audio');
	audio.src = 'https://youhaveowl.com/wp-content/uploads/2019/05/636254_Purple-Circle.mp3';
	audio.loop = true;


by (150 points)
hey thanks for your help man. this is exactly what i wanted