Merge pull request #260 from itsgareth/gareth/fix-ios-lockscreen-controls
feat: fix ios lockscreen controls
This commit is contained in:
commit
eb61e87980
1 changed files with 71 additions and 60 deletions
13
js/player.js
13
js/player.js
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue