feat: fix ios lockscreen controls

This commit is contained in:
Gareth Dawson 2026-02-28 18:58:50 +00:00
parent a7d0099c96
commit 523aa2d710

View file

@ -256,6 +256,7 @@ export class Player {
setupMediaSession() {
if (!('mediaSession' in navigator)) return;
const setHandlers = () => {
navigator.mediaSession.setActionHandler('play', async () => {
// Initialize and resume audio context first (required for iOS lock screen)
// Must happen before audio.play() or audio won't route through Web Audio
@ -298,15 +299,16 @@ export class Player {
this.playNext();
});
if (!this.isIOS) {
navigator.mediaSession.setActionHandler('seekbackward', (details) => {
const skipTime = details.seekOffset || 10;
this.seekBackward(skipTime);
});
navigator.mediaSession.setActionHandler('seekforward', (details) => {
const skipTime = details.seekOffset || 10;
this.seekForward(skipTime);
});
}
navigator.mediaSession.setActionHandler('seekto', (details) => {
if (details.seekTime !== undefined) {
@ -320,6 +322,15 @@ export class Player {
this.audio.currentTime = 0;
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) {