Persistant scene background looping sound

Hi, has anyone come accross an issue with loped scene audio file being persistant in any browser on any device even when the mobile is locked?!?! The only way to stop it is to close the browser. Any help would be appreciated.

1 Like

The basic way to do this is you need to listen to the page visibility change event and then pause audio.

Hype doesn’t have an API for play/pausing audio, so you will need to make a timeline that runs this action. The timeline can be triggered via JS.

I’d basically do these steps:

  1. Create a new timeline
  2. Add a timeline action onto that that is set to Stop Sound with whatever sound might be playing (you can chain a bunch together if there could be multiple)
  3. Create an On Scene Load action that Runs JavaScript
  4. Use code like this (replacing “StopAudio” with your timeline name from step #1):
	var handleVisibilityChange = (function () {
		if (document["hidden"]) {
			hypeDocument.startTimelineNamed("StopAudio");
		}

	});
	document.addEventListener("visibilitychange", handleVisibilityChange, false);

If you have multiple scenes you may need to change the listener to be removed on scene unload.

Here’s an example that loops audio and stops when the browser is not shown.

StopLoopingAudio.hype.zip (24.7 KB)

1 Like

Thanks Jonathan, much appreciated.