mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-03 01:42:50 +08:00
Fix silent playback on phones
Playback runs through a hidden <audio> element that only had autoplay set, which mobile browsers ignore for audible media, so it never started. Call play() on it from a user gesture (any tap/key, and the test tone button). Also listen for pointerup: on touch screens pointerdown is not a user activation, so resuming the AudioContext from it alone failed too. Reported via feedback (Chrome on Android, no voice or test tone) and reproduced with Firefox on Android. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU
This commit is contained in:
1 parent
bcdbabe201
commit
be7247f3f6
2 files changed
+16
-2
No files matched your search
+10
-2
@@ -7710,16 +7710,22 @@ function AppInner() {
|
|||||||
// Browsers keep an AudioContext created without a user gesture (auto-mic
|
// Browsers keep an AudioContext created without a user gesture (auto-mic
|
||||||
// on page load, a reconnect) "suspended" - silent - until one happens. The
|
// on page load, a reconnect) "suspended" - silent - until one happens. The
|
||||||
// mic permission prompt used to be the only thing resuming it, so with the
|
// mic permission prompt used to be the only thing resuming it, so with the
|
||||||
// mic denied or absent you'd never hear anyone. Any click/keypress does it.
|
// mic denied or absent you'd never hear anyone. Any click/keypress does it,
|
||||||
|
// and also starts the player's hidden <audio> element (see unlock()).
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const resume = () => {
|
const resume = () => {
|
||||||
const ctx = audioContextRef.current;
|
const ctx = audioContextRef.current;
|
||||||
if (ctx?.state === "suspended") ctx.resume().catch(() => {});
|
if (ctx?.state === "suspended") ctx.resume().catch(() => {});
|
||||||
|
audioPlayerRef.current?.unlock();
|
||||||
};
|
};
|
||||||
|
// pointerup too: on touch screens pointerdown doesn't count as a user
|
||||||
|
// gesture, so resume()/play() from it alone are refused on phones.
|
||||||
window.addEventListener("pointerdown", resume, true);
|
window.addEventListener("pointerdown", resume, true);
|
||||||
|
window.addEventListener("pointerup", resume, true);
|
||||||
window.addEventListener("keydown", resume, true);
|
window.addEventListener("keydown", resume, true);
|
||||||
return () => {
|
return () => {
|
||||||
window.removeEventListener("pointerdown", resume, true);
|
window.removeEventListener("pointerdown", resume, true);
|
||||||
|
window.removeEventListener("pointerup", resume, true);
|
||||||
window.removeEventListener("keydown", resume, true);
|
window.removeEventListener("keydown", resume, true);
|
||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
@@ -9554,7 +9560,9 @@ function AppInner() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handlePlayTestTone = () => {
|
const handlePlayTestTone = () => {
|
||||||
ensureAudioContext();
|
const ctx = ensureAudioContext();
|
||||||
|
if (ctx.state === "suspended") ctx.resume().catch(() => {});
|
||||||
|
audioPlayerRef.current?.unlock();
|
||||||
audioPlayerRef.current?.playTestTone();
|
audioPlayerRef.current?.playTestTone();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -370,6 +370,12 @@ export class AudioPlayer {
|
|||||||
document.body.appendChild(this.element);
|
document.body.appendChild(this.element);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Mobile browsers ignore `autoplay` for audible media, so the hidden
|
||||||
|
* element stays paused (silent) until play() runs inside a user gesture. */
|
||||||
|
unlock(): void {
|
||||||
|
if (this.element.paused) this.element.play().catch(() => {});
|
||||||
|
}
|
||||||
|
|
||||||
playFrame(base64Pcm: string): void {
|
playFrame(base64Pcm: string): void {
|
||||||
const int16 = base64ToInt16(base64Pcm);
|
const int16 = base64ToInt16(base64Pcm);
|
||||||
const frames = int16.length / 2;
|
const frames = int16.length / 2;
|
||||||
|
|||||||
Reference in new issue
Block a user