Merge pull request #260 from itsgareth/gareth/fix-ios-lockscreen-controls

feat: fix ios lockscreen controls
This commit is contained in:
edidealt 2026-03-01 12:23:08 +02:00 committed by GitHub
commit eb61e87980
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -256,6 +256,7 @@ export class Player {
setupMediaSession() { setupMediaSession() {
if (!('mediaSession' in navigator)) return; if (!('mediaSession' in navigator)) return;
const setHandlers = () => {
navigator.mediaSession.setActionHandler('play', async () => { navigator.mediaSession.setActionHandler('play', async () => {
// Initialize and resume audio context first (required for iOS lock screen) // Initialize and resume audio context first (required for iOS lock screen)
// Must happen before audio.play() or audio won't route through Web Audio // Must happen before audio.play() or audio won't route through Web Audio
@ -298,15 +299,16 @@ export class Player {
this.playNext(); this.playNext();
}); });
if (!this.isIOS) {
navigator.mediaSession.setActionHandler('seekbackward', (details) => { navigator.mediaSession.setActionHandler('seekbackward', (details) => {
const skipTime = details.seekOffset || 10; const skipTime = details.seekOffset || 10;
this.seekBackward(skipTime); this.seekBackward(skipTime);
}); });
navigator.mediaSession.setActionHandler('seekforward', (details) => { navigator.mediaSession.setActionHandler('seekforward', (details) => {
const skipTime = details.seekOffset || 10; const skipTime = details.seekOffset || 10;
this.seekForward(skipTime); this.seekForward(skipTime);
}); });
}
navigator.mediaSession.setActionHandler('seekto', (details) => { navigator.mediaSession.setActionHandler('seekto', (details) => {
if (details.seekTime !== undefined) { if (details.seekTime !== undefined) {
@ -320,6 +322,15 @@ export class Player {
this.audio.currentTime = 0; this.audio.currentTime = 0;
this.updateMediaSessionPlaybackState(); this.updateMediaSessionPlaybackState();
}); });
};
if (this.isIOS) {
// iOS: set handlers only when playback starts. Setting them in the constructor makes
// the lock screen show +10/-10. Registering on first 'playing' gives next/previous track
this.audio.addEventListener('playing', () => setHandlers(), { once: true });
} else {
setHandlers();
}
} }
setQuality(quality) { setQuality(quality) {