# Audio, pausing and ads

Several things can require your game to go quiet or to stop: the portal's mute setting, a video ad, the player clicking outside the game or switching tabs, and your own pause menu. Reviewers check that the game is silent and paused in each case and that it comes back correctly afterwards. This page states the contract and gives one small controller that implements it. It assumes you load and initialise the SDK as in the [SDK introduction](https://docs.zokogames.com/sdk/introduction).

## The contract

| Situation | How your game learns about it | Gameplay | Sound | Afterwards |
| --- | --- | --- | --- | --- |
| Portal mute | `SDK.game.settings.muteAudio` at start, then `SDK.game.addSettingsChangeListener` | Keeps running | Off while `muteAudio` is `true` | Sound returns only when the setting is `false` again |
| Video ad | Call `SDK.game.gameplayStop()` before `requestAd`; `adStarted` when it shows | Stopped | Off from `adStarted` | Restore in both `adFinished` and `adError`; call `gameplayStart()` when input is live again |
| Focus lost or tab hidden | The window's `blur` event, `visibilitychange` with `document.hidden` | Paused, with `gameplayStop()` | Off while the tab is hidden | Continue, or show your pause menu, and call `gameplayStart()` when play continues |
| Your pause menu | Your own button | Paused, with `gameplayStop()` | Your choice | `gameplayStart()` when the player resumes |
| Before the first gesture | Nothing yet | Not affected | Browsers block it | Create or `resume()` the `AudioContext` inside the first tap, click or key press |

The one rule that is easy to break: **never switch sound on blindly**. Sound is on only while none of the reasons to be silent holds. A game that sets the volume back to full when an ad finishes ignores a player who muted the portal, and reviewers test exactly that.

## A small controller

```js
// Sound is on only while no reason to be silent is active.
const SDK = window.ZokoGames.SDK;
const quiet = new Set(); // active reasons: "portal", "ad", "hidden"
let audioCtx = null;

function applyAudio() {
  if (!audioCtx) return;
  if (quiet.size > 0) audioCtx.suspend();
  else audioCtx.resume();
}

function setQuiet(reason, on) {
  if (on) quiet.add(reason);
  else quiet.delete(reason);
  applyAudio();
}

// 1. Browsers allow sound only after a gesture, so create the context in the first one.
function unlockAudio() {
  audioCtx = audioCtx || new AudioContext();
  applyAudio();
}
window.addEventListener("pointerdown", unlockAudio, { once: true });
window.addEventListener("keydown", unlockAudio, { once: true });

// 2. The portal's mute setting, at start and on every change.
async function startSdk() {
  await SDK.init(); // never rejects
  if (SDK.environment === "disabled") return;
  setQuiet("portal", SDK.game.settings.muteAudio);
  SDK.game.addSettingsChangeListener((settings) => setQuiet("portal", settings.muteAudio));
}

// 3. Losing focus pauses play; a hidden tab is also silent.
window.addEventListener("blur", () => pauseGame()); // your own pause, which calls gameplayStop()
document.addEventListener("visibilitychange", () => setQuiet("hidden", document.hidden));

// 4. Ads: gameplay stops first, sound is off during the ad, and both outcomes restore it.
async function showAd(type) {
  if (SDK.environment === "disabled") return false;
  let finished = false;
  try {
    await SDK.game.gameplayStop();
    await SDK.ad.requestAd(type, {
      adStarted: () => setQuiet("ad", true),
      adFinished: () => { finished = true; },
      adError: (error) => console.info("No ad:", error.code),
    });
  } catch (error) {
    console.warn("SDK:", error.code); // sdkNotInitialized, sdkDisabled or invalidArgument
  } finally {
    setQuiet("ad", false); // after adFinished, adError or a rejection
  }
  return finished; // grant a reward only when this is true
}

startSdk();
```

Call `gameplayStart()` yourself when the player is in control again: at the start of the next round, or when they leave your pause menu. Background music and effects all go through the one `AudioContext`, so suspending it silences both. If you use an audio library, put its mute switch where `applyAudio` suspends and resumes the context.

For an ad-free member, a midgame request runs `adStarted` and `adFinished` at once without showing anything, so the same code path resumes the game straight away. Rewarded ads still play for them.

## Engines

The contract is the same in every engine. Each [engine page](https://docs.zokogames.com/engines/overview) shows how its wrapper reports the start and end of an ad, and the portal settings where the wrapper exposes them. For Three.js, PixiJS and plain canvas games, [Plain HTML5, Three.js and PixiJS](https://docs.zokogames.com/engines/html5-libraries) shows where each library's pause and volume switches are.

## Test it

- [ ] Reload the game: nothing plays before your first tap, click or key press.
- [ ] Locally with `?muteAudio=true`, or with **Platform mute is on** in the build preview's **Simulate the player** card: the game is silent at once and stays silent after an ad.
- [ ] Turn **Platform mute is on** off again: sound comes back without a reload.
- [ ] Click outside the game and switch tabs during a round: play stops and the event log shows `gameplayStop`.
- [ ] Locally with `?ad_response=adblock`, or with **Ad blocker installed**: the game continues with sound, and no reward is granted.
- [ ] Watch a whole ad: the game is silent during it and play continues after it, with `gameplayStart` in the log.

## Next steps

- [Video ads](https://docs.zokogames.com/sdk/ads)
- [Advertisement rules](https://docs.zokogames.com/requirements/advertisements)
- [Mobile, touch and orientation](https://docs.zokogames.com/guides/mobile)
