Showing music detail on iphone

This commit is contained in:
2025-10-30 16:23:02 +11:00
parent 58ef625bd8
commit af4a3bb50e
+78
View File
@@ -107,6 +107,84 @@ export default function Player({
}
}, [audioRef])
// Media Session API for lock screen controls (iOS, Android)
useEffect(() => {
if (!currentMusic || !('mediaSession' in navigator)) return
const getAbsoluteImageUrl = (thumbnail: string | null) => {
if (!thumbnail) return undefined
if (thumbnail.startsWith('http')) return thumbnail
// Convert relative path to absolute URL
const baseUrl = window.location.origin
return `${baseUrl}/music/${thumbnail}`
}
// Set metadata for lock screen
navigator.mediaSession.metadata = new MediaMetadata({
title: currentMusic.title,
artist: currentMusic.artist || 'Unknown Artist',
album: currentMusic.album || '',
artwork: currentMusic.thumbnail ? [
{ src: getAbsoluteImageUrl(currentMusic.thumbnail), sizes: '512x512', type: 'image/jpeg' },
] : undefined,
})
// Set up action handlers
navigator.mediaSession.setActionHandler('play', () => {
onTogglePlay()
})
navigator.mediaSession.setActionHandler('pause', () => {
onTogglePlay()
})
navigator.mediaSession.setActionHandler('previoustrack', () => {
onPrevious()
})
navigator.mediaSession.setActionHandler('nexttrack', () => {
onNext()
})
navigator.mediaSession.setActionHandler('seekto', (details) => {
if (details.seekTime && audioRef.current) {
audioRef.current.currentTime = details.seekTime
setCurrentTime(details.seekTime)
}
})
// Update position state
const updatePositionState = () => {
if (audioRef.current && !isNaN(audioRef.current.duration)) {
try {
navigator.mediaSession.setPositionState({
duration: audioRef.current.duration,
playbackRate: audioRef.current.playbackRate,
position: audioRef.current.currentTime,
})
} catch (e) {
// Ignore errors on browsers that don't fully support position state
}
}
}
// Update position state on time update
const audio = audioRef.current
if (audio) {
audio.addEventListener('timeupdate', updatePositionState)
audio.addEventListener('loadedmetadata', updatePositionState)
audio.addEventListener('play', updatePositionState)
audio.addEventListener('pause', updatePositionState)
return () => {
audio.removeEventListener('timeupdate', updatePositionState)
audio.removeEventListener('loadedmetadata', updatePositionState)
audio.removeEventListener('play', updatePositionState)
audio.removeEventListener('pause', updatePositionState)
}
}
}, [currentMusic, isPlaying, onTogglePlay, onNext, onPrevious, audioRef])
const handleSeek = (value: number[]) => {
if (audioRef.current) {
audioRef.current.currentTime = value[0]