Musica su iPhone e iPad: il volume lì è di sola lettura, la dissolvenza non arrivava mai a zero e il brano vecchio restava sotto quello nuovo (organo e paese insieme nella Pieve). Il livello si tiene a parte e lo zero diventa muto; test che simula iOS
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018ks2K99uqCZjAxvy7Rdj1R
This commit is contained in:
+16
-6
@@ -19,6 +19,12 @@ export function trackFor(story, { screen, scene, phase }) {
|
||||
|
||||
const FADE = 1.2;
|
||||
|
||||
// Dove il volume non si può cambiare (iOS), almeno il muto e lo zero funzionano: `muted` lì si rispetta.
|
||||
function setLevel(a) {
|
||||
a.volume = a.level;
|
||||
a.muted = a.level === 0;
|
||||
}
|
||||
|
||||
export class Music {
|
||||
constructor({ muted = false, volume = 0.6 } = {}) {
|
||||
this.muted = muted;
|
||||
@@ -46,22 +52,26 @@ export class Music {
|
||||
if (this.current) this.old.push(this.current.audio);
|
||||
this.current = src ? { src, audio: this.start(src) } : null;
|
||||
}
|
||||
// Il brano in corso sale (o scende) verso il volume scelto; quelli vecchi sfumano fino a zero.
|
||||
// Il brano in corso sale (o scende) verso il volume scelto; quelli vecchi sfumano fino a zero, e lì si fermano.
|
||||
// Il livello si tiene in `level`, non si rilegge da audio.volume: su iPhone e iPad quello è di sola lettura e
|
||||
// resta sempre 1, e un brano vecchio non arrivava mai a zero (in chiesa l'organo suonava insieme al paese).
|
||||
const step = dt / FADE, target = this.muted || this.hold ? 0 : this.volume * this.duck;
|
||||
if (this.current) {
|
||||
const a = this.current.audio;
|
||||
a.volume = a.volume < target ? Math.min(target, a.volume + step) : Math.max(target, a.volume - step);
|
||||
if (this.hold && a.volume === 0 && !a.paused) a.pause();
|
||||
a.level = a.level < target ? Math.min(target, a.level + step) : Math.max(target, a.level - step);
|
||||
setLevel(a);
|
||||
if (this.hold && a.level === 0 && !a.paused) a.pause();
|
||||
else if (!this.hold && !this.away && a.paused && a !== this.blocked) a.play().catch(() => { this.blocked = a; });
|
||||
}
|
||||
for (const a of this.old) a.volume = Math.max(0, a.volume - step);
|
||||
this.old = this.old.filter(a => a.volume > 0 || (a.pause(), false));
|
||||
for (const a of this.old) { a.level = Math.max(0, a.level - step); setLevel(a); }
|
||||
this.old = this.old.filter(a => a.level > 0 || (a.pause(), false));
|
||||
}
|
||||
|
||||
start(src) {
|
||||
const audio = new Audio(versioned(src));
|
||||
audio.loop = true;
|
||||
audio.volume = 0;
|
||||
audio.level = 0;
|
||||
setLevel(audio);
|
||||
audio.play().catch(() => { this.blocked = audio; }); // senza permesso si riprova al primo gesto
|
||||
return audio;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user