Skip to content

Commit

Permalink
add support for global play/pause media keys.
Browse files Browse the repository at this point in the history
  • Loading branch information
alechko committed Mar 9, 2022
1 parent fe31b38 commit 68300f0
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions src/components/player/controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,38 @@ export const Controls = (props: BoxProps) => {
})
.catch(e => console.error(e))
}
if (navigator.mediaSession) {
navigator.mediaSession.setActionHandler('play', () => {
dispatch({
type: station ? 'play' : 'resume',
payload: station ? { data: station } : {},
})
})
navigator.mediaSession.setActionHandler('pause', () => {
dispatch({
type: 'pause',
})
})
}
}, [])

const playNext = () => {
const index = stations.findIndex(v => v.id === station!.id)
index &&
dispatch({
type: 'play',
payload: { data: stations[index + 1] },
})
}

const playPrev = () => {
const index = stations.findIndex(v => v.id === station!.id)
index &&
dispatch({
type: 'play',
payload: { data: stations[index - 1] },
})
}
return (
<Box {...props}>
<Player />
Expand Down Expand Up @@ -96,13 +126,7 @@ export const Controls = (props: BoxProps) => {
aria-label="Prev"
icon={<Icon as={Prev} />}
disabled={!playing}
onClick={() => {
const index = stations.findIndex(v => v.id === station!.id)
dispatch({
type: 'play',
payload: { data: stations[index - 1] },
})
}}
onClick={playPrev}
/>
{playing ? (
<IconButton
Expand Down Expand Up @@ -133,13 +157,7 @@ export const Controls = (props: BoxProps) => {
aria-label="Next"
icon={<Icon as={Next} />}
disabled={!playing}
onClick={() => {
const index = stations.findIndex(v => v.id === station!.id)
dispatch({
type: 'play',
payload: { data: stations[index + 1] },
})
}}
onClick={playNext}
/>
<Slider
aria-label="Volume"
Expand Down

0 comments on commit 68300f0

Please sign in to comment.