All files / app-play/src silent-mode-playback.ts

56.57% Statements 43/76
67.85% Branches 19/28
63.63% Functions 14/22
58.2% Lines 39/67

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262                    9x 9x       20x       5x             8x 8x               15x 15x   2x         6x 6x             4x 4x     1x         5x 5x                                       6x             4x                     9x                                                                         9x               7x 7x 11x   7x   7x 7x 2x 6x 2x   21x 7x 2x   7x 7x 7x 3x 1x 1x                                                                                   9x                                                                                                            
import { optionDefault } from './option-defaults.ts';
import { html, nothing, render } from 'lit';
// The player's "Play in silent mode" setting: whether the page asks the platform audio session (`navigator.audioSession`,
// Safari / iOS) for `'playback'`, so sound plays with the Ring / Silent switch on — at the cost of pausing music
// playing in other apps. On by default (a rhythm game with no sound is unplayable); turned off it restores the
// browser's own `'auto'`. Offered on the start card and in the Options dialog. Also the one-time hint pointing iPhone /
// iPad users at the setting when sound starts with it off.
 
import { applySilentModePlayback, type SystemAudioSessionHost } from '@be-music/player-browser/audio';
 
export const PLAY_IN_SILENT_MODE_STORAGE_KEY = 'be-music.play-in-silent-mode';
export const SILENT_MODE_HINT_SEEN_STORAGE_KEY = 'be-music.silent-mode-hint-seen';
 
/** Parses the stored setting: only `'off'` turns it off, anything else (nothing stored, junk) keeps the default on. */
export function parseStoredPlayInSilentMode(raw: string | null | undefined): boolean {
  return raw === 'off' ? false : raw === 'on' ? true : optionDefault('playInSilentMode');
}
 
export function serializePlayInSilentMode(enabled: boolean): string {
  return enabled ? 'on' : 'off';
}
 
/** Storage the player reads settings from: `localStorage`, or a stand-in in tests. */
export type SettingStorage = Pick<Storage, 'getItem' | 'setItem'>;
 
function defaultStorage(): SettingStorage | undefined {
  try {
    return typeof window === 'undefined' ? undefined : window.localStorage;
  } catch {
    return undefined;
  }
}
 
/** The stored setting; on when nothing is stored or storage is blocked. */
export function readPlayInSilentMode(storage: SettingStorage | undefined = defaultStorage()): boolean {
  try {
    return parseStoredPlayInSilentMode(storage?.getItem(PLAY_IN_SILENT_MODE_STORAGE_KEY));
  } catch {
    return optionDefault('playInSilentMode');
  }
}
 
export function storePlayInSilentMode(enabled: boolean, storage: SettingStorage | undefined = defaultStorage()): void {
  try {
    storage?.setItem(PLAY_IN_SILENT_MODE_STORAGE_KEY, serializePlayInSilentMode(enabled));
  } catch {
    // Private windows / blocked storage: the choice still applies for this session.
  }
}
 
export function readSilentModeHintSeen(storage: SettingStorage | undefined = defaultStorage()): boolean {
  try {
    return storage?.getItem(SILENT_MODE_HINT_SEEN_STORAGE_KEY) === '1';
  } catch {
    // Unreadable storage: treat as seen, so a blocked store never shows the hint on every visit.
    return true;
  }
}
 
export function storeSilentModeHintSeen(storage: SettingStorage | undefined = defaultStorage()): void {
  try {
    storage?.setItem(SILENT_MODE_HINT_SEEN_STORAGE_KEY, '1');
  } catch {
    // Blocked storage: the hint stays hidden for this session anyway.
  }
}
 
/** What decides whether the "No sound?" hint shows when sound first starts. */
export interface SilentModeHintInput {
  /** The browser has an audio session (Safari / iOS); elsewhere the setting does not exist. */
  readonly supported: boolean;
  /** The setting is already on. */
  readonly enabled: boolean;
  /** The hint was shown before (once per browser). */
  readonly seen: boolean;
  /** A phone or tablet — only those have a silent switch that mutes web audio. */
  readonly touchFirst: boolean;
}
 
/** Show the hint once, on a touch-first device whose browser supports the setting, while the setting is off. */
export function shouldShowSilentModeHint(input: SilentModeHintInput): boolean {
  return input.supported && input.touchFirst && !input.enabled && !input.seen;
}
 
export type SilentModeLanguage = 'en' | 'ja';
 
/** The UI language for the setting and hint: Japanese when the browser prefers it, else English. */
export function resolveSilentModeLanguage(language: string | undefined): SilentModeLanguage {
  return language?.toLowerCase().startsWith('ja') ? 'ja' : 'en';
}
 
export interface SilentModeStrings {
  readonly label: string;
  readonly note: string;
  readonly hint: string;
  readonly hintAction: string;
  readonly hintDismiss: string;
}
 
export const SILENT_MODE_STRINGS: Readonly<Record<SilentModeLanguage, SilentModeStrings>> = {
  en: {
    label: 'Play in silent mode',
    note: 'Plays sound even when the iPhone / iPad is in silent mode. Pauses music playing in other apps.',
    hint: 'No sound? Your device may be in silent mode.',
    hintAction: 'Play in silent mode',
    hintDismiss: 'Dismiss',
  },
  ja: {
    label: '消音モードでも再生',
    note: 'iPhone / iPad が消音モードでも音を鳴らします。他のアプリで再生中の音楽は停止します。',
    hint: '音が出ない場合は、消音モードになっているかもしれません。',
    hintAction: '消音モードでも再生',
    hintDismiss: '閉じる',
  },
};
 
/** The page events {@link installSilentModePlayback} listens to. */
export interface SilentModeEventTarget {
  addEventListener(type: string, listener: () => void, options?: AddEventListenerOptions): void;
  removeEventListener(type: string, listener: () => void, options?: EventListenerOptions): void;
}
 
export interface InstallSilentModePlaybackOptions {
  /** The current setting. */
  readonly enabled: () => boolean;
  /** Where the user gestures and `pageshow` land (`window`). */
  readonly target: SilentModeEventTarget;
  /** Where `visibilitychange` lands (`document`), and whether the page is visible. */
  readonly visibility?: SilentModeEventTarget & { readonly visibilityState?: DocumentVisibilityState };
  /** The `navigator` (or a stand-in) holding the audio session. */
  readonly host?: SystemAudioSessionHost;
  /** Called once, on the first user gesture — the moment browsers let audio start. */
  readonly onFirstGesture?: () => void;
}
 
/** The user gestures that unlock audio in the browser (the scenes resume their `AudioContext` from these). */
export const AUDIO_UNLOCK_EVENTS: readonly string[] = ['pointerdown', 'keydown', 'touchend'];
 
/**
 * Applies the setting now (before any `AudioContext` exists) and again whenever the platform may activate the audio
 * session afresh: the first user gesture (when the scenes resume their contexts), returning to the page, and a page
 * restored from the back/forward cache. Returns a function that removes the listeners.
 */
export function installSilentModePlayback(options: InstallSilentModePlaybackOptions): () => void {
  const { target, visibility, host } = options;
  const apply = () => {
    applySilentModePlayback(options.enabled(), host);
  };
  apply();
  // Capture phase, so the type is set before a scene's own handler resumes its context on the same gesture.
  const listenerOptions: AddEventListenerOptions = { capture: true };
  const onGesture = () => {
    apply();
    for (const type of AUDIO_UNLOCK_EVENTS) target.removeEventListener(type, onGesture, listenerOptions);
    options.onFirstGesture?.();
  };
  for (const type of AUDIO_UNLOCK_EVENTS) target.addEventListener(type, onGesture, listenerOptions);
  const onVisibilityChange = () => {
    if (visibility?.visibilityState !== 'hidden') apply();
  };
  visibility?.addEventListener('visibilitychange', onVisibilityChange);
  target.addEventListener('pageshow', apply);
  return () => {
    for (const type of AUDIO_UNLOCK_EVENTS) target.removeEventListener(type, onGesture, listenerOptions);
    visibility?.removeEventListener('visibilitychange', onVisibilityChange);
    target.removeEventListener('pageshow', apply);
  };
}
 
export interface StartSilentModeOptionOptions {
  /** The start card's `#start-silent-mode` section, hidden in the markup until the browser offers the setting. */
  readonly section: HTMLElement;
  readonly strings: SilentModeStrings;
  /** The current setting. */
  readonly checked: boolean;
  /** The player ticked or unticked the box. */
  readonly onChange: (checked: boolean) => void;
}
 
/** The start card's "Play in silent mode" checkbox, kept in step with the Options dialog's. */
export interface StartSilentModeOption {
  setChecked(checked: boolean): void;
}
 
/**
 * Fills and shows the start card's "Play in silent mode" row: the checkbox and a one-line note on what it costs. Call
 * it only where the browser has an audio session; elsewhere the section stays hidden.
 */
export function mountStartSilentModeOption(options: StartSilentModeOptionOptions): StartSilentModeOption | undefined {
  const { section, strings } = options;
  const input = section.querySelector<HTMLInputElement>('input[type="checkbox"]');
  if (input === null) return undefined;
  const label = section.querySelector<HTMLElement>('[data-silent-mode-label]');
  const note = section.querySelector<HTMLElement>('[data-silent-mode-note]');
  if (label !== null) render(strings.label, label);
  if (note !== null) render(strings.note, note);
  input.checked = options.checked;
  input.addEventListener('change', () => options.onChange(input.checked));
  section.hidden = false;
  return {
    setChecked: (checked) => {
      input.checked = checked;
    },
  };
}
 
/** How long the hint stays up before it fades out by itself. */
export const SILENT_MODE_HINT_DURATION_MS = 9000;
 
export interface SilentModeHintOptions {
  readonly strings: SilentModeStrings;
  /** Turns the setting on (the hint's action button). */
  readonly onEnable: () => void;
  readonly parent?: HTMLElement;
}
 
/**
 * Shows the "No sound?" hint: a small card at the bottom of the screen with a button that turns the setting on and a
 * dismiss button. It never blocks the stage and leaves by itself after {@link SILENT_MODE_HINT_DURATION_MS}.
 */
export function mountSilentModeHint(options: SilentModeHintOptions): void {
  const { strings } = options;
  const card = document.createElement('div');
  card.className = 'card silent-mode-hint gap-2 px-3 py-2';
  card.setAttribute('role', 'status');
  let timer = 0;
  const close = () => {
    window.clearTimeout(timer);
    render(nothing, card);
    card.remove();
  };
  // Taps on the card are not lane presses or scene clicks.
  card.addEventListener('pointerdown', (event) => event.stopPropagation());
  render(
    html`<span class="silent-mode-hint-text text-sm">${strings.hint}</span>
      <button
        class="btn"
        type="button"
        data-size="sm"
        @click=${() => {
          options.onEnable();
          close();
        }}
      >
        ${strings.hintAction}
      </button>
      <button
        class="btn"
        type="button"
        data-variant="ghost"
        data-size="icon-sm"
        aria-label=${strings.hintDismiss}
        @click=${close}
      >
        ×
      </button>`,
    card,
  );
  timer = window.setTimeout(close, SILENT_MODE_HINT_DURATION_MS);
  (options.parent ?? document.body).append(card);
}