mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 08:37:22 +00:00
Play dog whistle sine wave to make persistent notification display on Android
This commit is contained in:
2 files changed
+80
-58
No files matched your search
Binary file not shown.
+80
-58
@@ -12,7 +12,7 @@ interface UseKeepAliveOptions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hook to keep the tab alive using a silent sine wave and Media Session API.
|
* Hook to keep the tab alive using a silent audio element and Media Session API.
|
||||||
* This prevents the browser from throttling the tab when the user locks their phone
|
* This prevents the browser from throttling the tab when the user locks their phone
|
||||||
* or navigates away, allowing AI processing to continue in the background.
|
* or navigates away, allowing AI processing to continue in the background.
|
||||||
*/
|
*/
|
||||||
@@ -22,50 +22,29 @@ export function useKeepAlive({
|
|||||||
artist,
|
artist,
|
||||||
artwork = []
|
artwork = []
|
||||||
}: UseKeepAliveOptions) {
|
}: UseKeepAliveOptions) {
|
||||||
const audioContextRef = useRef<AudioContext | null>(null);
|
const audioElementRef = useRef<HTMLAudioElement | null>(null);
|
||||||
const oscillatorRef = useRef<OscillatorNode | null>(null);
|
|
||||||
const gainNodeRef = useRef<GainNode | null>(null);
|
|
||||||
const isActiveRef = useRef(false);
|
const isActiveRef = useRef(false);
|
||||||
|
|
||||||
const startKeepAlive = useCallback(async () => {
|
const startKeepAlive = useCallback(async () => {
|
||||||
if (isActiveRef.current || !enabled) return;
|
if (isActiveRef.current || !enabled) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Create audio context
|
// Create audio element and add it to the DOM
|
||||||
const AudioContextClass = window.AudioContext || (window as typeof window & { webkitAudioContext?: typeof AudioContext }).webkitAudioContext;
|
audioElementRef.current = document.createElement('audio');
|
||||||
if (!AudioContextClass) {
|
const audio = audioElementRef.current;
|
||||||
throw new Error('AudioContext not supported');
|
|
||||||
}
|
|
||||||
audioContextRef.current = new AudioContextClass();
|
|
||||||
const audioContext = audioContextRef.current;
|
|
||||||
|
|
||||||
// Resume audio context if suspended (required for user interaction)
|
// Configure audio element to use the provided audio file
|
||||||
if (audioContext.state === 'suspended') {
|
audio.src = '/sine.mp3';
|
||||||
await audioContext.resume();
|
audio.loop = true;
|
||||||
}
|
audio.volume = 0.01; // Very low volume but not completely muted
|
||||||
|
audio.preload = 'auto';
|
||||||
|
audio.style.display = 'none'; // Hide the audio element
|
||||||
|
audio.setAttribute('aria-hidden', 'true'); // Hide from screen readers
|
||||||
|
|
||||||
// Create oscillator for silent sine wave
|
// Add to DOM (required for proper media session support)
|
||||||
oscillatorRef.current = audioContext.createOscillator();
|
document.body.appendChild(audio);
|
||||||
gainNodeRef.current = audioContext.createGain();
|
|
||||||
|
|
||||||
const oscillator = oscillatorRef.current;
|
// Set up Media Session API before playing
|
||||||
const gainNode = gainNodeRef.current;
|
|
||||||
|
|
||||||
// Configure oscillator
|
|
||||||
oscillator.type = 'sine';
|
|
||||||
oscillator.frequency.setValueAtTime(20000, audioContext.currentTime); // 20kHz - above human hearing range
|
|
||||||
|
|
||||||
// Set volume to nearly silent but not completely muted
|
|
||||||
gainNode.gain.setValueAtTime(0.001, audioContext.currentTime);
|
|
||||||
|
|
||||||
// Connect nodes
|
|
||||||
oscillator.connect(gainNode);
|
|
||||||
gainNode.connect(audioContext.destination);
|
|
||||||
|
|
||||||
// Start oscillator
|
|
||||||
oscillator.start();
|
|
||||||
|
|
||||||
// Set up Media Session API
|
|
||||||
if ('mediaSession' in navigator) {
|
if ('mediaSession' in navigator) {
|
||||||
navigator.mediaSession.metadata = new MediaMetadata({
|
navigator.mediaSession.metadata = new MediaMetadata({
|
||||||
title,
|
title,
|
||||||
@@ -73,25 +52,72 @@ export function useKeepAlive({
|
|||||||
artwork
|
artwork
|
||||||
});
|
});
|
||||||
|
|
||||||
// Set playback state
|
|
||||||
navigator.mediaSession.playbackState = 'playing';
|
|
||||||
|
|
||||||
// Handle media session actions
|
// Handle media session actions
|
||||||
navigator.mediaSession.setActionHandler('play', () => {
|
navigator.mediaSession.setActionHandler('play', () => {
|
||||||
// Already playing, do nothing
|
if (audioElementRef.current) {
|
||||||
|
audioElementRef.current.play().catch(console.error);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
navigator.mediaSession.setActionHandler('pause', () => {
|
navigator.mediaSession.setActionHandler('pause', () => {
|
||||||
stopKeepAlive();
|
if (audioElementRef.current) {
|
||||||
|
audioElementRef.current.pause();
|
||||||
|
audioElementRef.current.currentTime = 0;
|
||||||
|
audioElementRef.current.src = '';
|
||||||
|
audioElementRef.current.load();
|
||||||
|
|
||||||
|
// Remove from DOM
|
||||||
|
if (audioElementRef.current.parentNode) {
|
||||||
|
audioElementRef.current.parentNode.removeChild(audioElementRef.current);
|
||||||
|
}
|
||||||
|
|
||||||
|
audioElementRef.current = null;
|
||||||
|
}
|
||||||
|
if ('mediaSession' in navigator) {
|
||||||
|
navigator.mediaSession.metadata = null;
|
||||||
|
navigator.mediaSession.playbackState = 'none';
|
||||||
|
navigator.mediaSession.setActionHandler('play', null);
|
||||||
|
navigator.mediaSession.setActionHandler('pause', null);
|
||||||
|
navigator.mediaSession.setActionHandler('stop', null);
|
||||||
|
}
|
||||||
|
isActiveRef.current = false;
|
||||||
});
|
});
|
||||||
|
|
||||||
navigator.mediaSession.setActionHandler('stop', () => {
|
navigator.mediaSession.setActionHandler('stop', () => {
|
||||||
stopKeepAlive();
|
if (audioElementRef.current) {
|
||||||
|
audioElementRef.current.pause();
|
||||||
|
audioElementRef.current.currentTime = 0;
|
||||||
|
audioElementRef.current.src = '';
|
||||||
|
audioElementRef.current.load();
|
||||||
|
|
||||||
|
// Remove from DOM
|
||||||
|
if (audioElementRef.current.parentNode) {
|
||||||
|
audioElementRef.current.parentNode.removeChild(audioElementRef.current);
|
||||||
|
}
|
||||||
|
|
||||||
|
audioElementRef.current = null;
|
||||||
|
}
|
||||||
|
if ('mediaSession' in navigator) {
|
||||||
|
navigator.mediaSession.metadata = null;
|
||||||
|
navigator.mediaSession.playbackState = 'none';
|
||||||
|
navigator.mediaSession.setActionHandler('play', null);
|
||||||
|
navigator.mediaSession.setActionHandler('pause', null);
|
||||||
|
navigator.mediaSession.setActionHandler('stop', null);
|
||||||
|
}
|
||||||
|
isActiveRef.current = false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Start playing
|
||||||
|
await audio.play();
|
||||||
|
|
||||||
|
// Set playback state after successful play
|
||||||
|
if ('mediaSession' in navigator) {
|
||||||
|
navigator.mediaSession.playbackState = 'playing';
|
||||||
|
}
|
||||||
|
|
||||||
isActiveRef.current = true;
|
isActiveRef.current = true;
|
||||||
console.log('Keep-alive started with silent audio and media session');
|
console.log('Keep-alive started with silent audio element and media session');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to start keep-alive:', error);
|
console.error('Failed to start keep-alive:', error);
|
||||||
}
|
}
|
||||||
@@ -101,23 +127,19 @@ export function useKeepAlive({
|
|||||||
if (!isActiveRef.current) return;
|
if (!isActiveRef.current) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Stop oscillator
|
// Stop and cleanup audio element
|
||||||
if (oscillatorRef.current) {
|
if (audioElementRef.current) {
|
||||||
oscillatorRef.current.stop();
|
audioElementRef.current.pause();
|
||||||
oscillatorRef.current.disconnect();
|
audioElementRef.current.currentTime = 0;
|
||||||
oscillatorRef.current = null;
|
audioElementRef.current.src = '';
|
||||||
}
|
audioElementRef.current.load(); // Reset the audio element
|
||||||
|
|
||||||
// Disconnect gain node
|
// Remove from DOM
|
||||||
if (gainNodeRef.current) {
|
if (audioElementRef.current.parentNode) {
|
||||||
gainNodeRef.current.disconnect();
|
audioElementRef.current.parentNode.removeChild(audioElementRef.current);
|
||||||
gainNodeRef.current = null;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Close audio context
|
audioElementRef.current = null;
|
||||||
if (audioContextRef.current) {
|
|
||||||
audioContextRef.current.close();
|
|
||||||
audioContextRef.current = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear media session
|
// Clear media session
|
||||||
|
|||||||
Reference in new issue
Block a user