Skip to content

Commit

Permalink
ref: add media metadata & misc improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
pfrlv committed Sep 23, 2022
1 parent 6f81349 commit dbe3cac
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 24 deletions.
2 changes: 1 addition & 1 deletion components/Footer.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default function Footer() {
return (
<div className="editor text-[11px] leading-[15px] text-white/60 sm:text-gray-400 w-[300px] sm:w-auto sm:max-w-[250px] px-[10px]">
<div className="editor text-[11px] leading-[15px] text-white sm:text-black w-[300px] sm:w-auto sm:max-w-[250px] px-[10px]">
Simple web app for background music from the 
<a href="https://boxradio.net/" rel="noopener noreferrer" target="_blank">
Box Radio
Expand Down
60 changes: 41 additions & 19 deletions hooks/use-stream-audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,58 @@ function useStreamAudio() {
const { playing, canplay } = useSnapshot(state)
const audio = useRef()

const play = useCallback(async () => {
if (!canplay) return
await audio.current.play()
}, [canplay])

const pause = useCallback(() => {
if (!canplay) return
audio.current.pause()
}, [canplay])

useEffect(() => {
audio.current = new Audio(STREAM_URL)
}, [])

useEffect(() => {
const onCanPlay = () => {
state.canplay = true
if ('mediaSession' in navigator) {
navigator.mediaSession.metadata = new MediaMetadata({
title: 'Lo-Fi, Chillhop, ChillJazz, Sleep Music, Work Music etc.',
artist: 'LoFi Radio',
artwork: [
{ src: '/mediasession/96.png', sizes: '96x96', type: 'image/png' },
{ src: '/mediasession/128.png', sizes: '128x128', type: 'image/png' },
{ src: '/mediasession/192.png', sizes: '192x192', type: 'image/png' },
{ src: '/mediasession/256.png', sizes: '256x256', type: 'image/png' },
{ src: '/mediasession/384.png', sizes: '384x384', type: 'image/png' },
{ src: '/mediasession/512.png', sizes: '512x512', type: 'image/png' }
]
})
}
}, [])

audio.current.addEventListener('loadedmetadata', onCanPlay)
useEffect(() => {
const onPause = () => (state.playing = false)
audio.current.addEventListener('pause', onPause)

return () => {
audio.current.pause()
audio.current.removeEventListener('loadedmetadata', onCanPlay)
}
return () => audio.current.removeEventListener('pause', onPause)
}, [])

const play = useCallback(() => {
if (!canplay) return
useEffect(() => {
const onPlay = () => (state.playing = true)
audio.current.addEventListener('play', onPlay)

audio.current.play()
state.playing = true
}, [canplay])
return () => audio.current.removeEventListener('play', onPlay)
}, [])

const pause = useCallback(() => {
if (!canplay) return
useEffect(() => {
const onCanplay = () => (state.canplay = true)
audio.current.addEventListener('loadedmetadata', onCanplay)

audio.current.pause()
state.playing = false
}, [canplay])
return () => {
audio.current.pause()
audio.current.removeEventListener('loadedmetadata', onCanplay)
}
}, [])

return {
canplay,
Expand Down
Binary file added public/mediasession/128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/mediasession/192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/mediasession/256.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/mediasession/384.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/mediasession/512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/mediasession/96.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 1 addition & 4 deletions styles/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@

@layer components {
.editor a {
@apply text-white/80 sm:text-gray-600 hover:text-white sm:hover:text-gray-900 transition-colors;
}
.editor svg {
@apply inline-block -mt-[2.5px];
@apply text-white/50 sm:text-black/50 hover:text-white sm:hover:text-black transition-colors;
}
[data-plofier] {
animation: Rotation 24s linear infinite;
Expand Down

1 comment on commit dbe3cac

@vercel
Copy link

@vercel vercel bot commented on dbe3cac Sep 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.