|
1 | 1 | 'use client'; |
2 | 2 |
|
3 | | -import React, { createContext, useContext, useState, useRef, useEffect } from 'react'; |
| 3 | +import React, { createContext, useContext, useState, useRef, useEffect, useCallback } from 'react'; |
4 | 4 |
|
5 | 5 | export interface Track { |
6 | 6 | id: number; |
@@ -32,6 +32,45 @@ export function AudioProvider({ children, allTracks }: { children: React.ReactNo |
32 | 32 | const [progress, setProgress] = useState(0); |
33 | 33 | const audioRef = useRef<HTMLAudioElement | null>(null); |
34 | 34 |
|
| 35 | + const playTrack = useCallback((track: Track) => { |
| 36 | + if (audioRef.current) { |
| 37 | + setIsLoading(true); // Set loading immediately |
| 38 | + audioRef.current.src = track.url; |
| 39 | + audioRef.current.play().catch(e => { |
| 40 | + console.error("Playback failed:", e); |
| 41 | + setIsLoading(false); |
| 42 | + }); |
| 43 | + setCurrentTrack(track); |
| 44 | + setIsPlaying(true); |
| 45 | + } |
| 46 | + }, []); |
| 47 | + |
| 48 | + const togglePlay = useCallback(() => { |
| 49 | + if (audioRef.current) { |
| 50 | + if (isPlaying) { |
| 51 | + audioRef.current.pause(); |
| 52 | + setIsPlaying(false); |
| 53 | + } else { |
| 54 | + audioRef.current.play().catch(console.error); |
| 55 | + setIsPlaying(true); |
| 56 | + } |
| 57 | + } |
| 58 | + }, [isPlaying]); |
| 59 | + |
| 60 | + const playNext = useCallback(() => { |
| 61 | + if (!currentTrack) return; |
| 62 | + const currentIndex = allTracks.findIndex(t => t.id === currentTrack.id); |
| 63 | + const nextIndex = (currentIndex + 1) % allTracks.length; |
| 64 | + playTrack(allTracks[nextIndex]); |
| 65 | + }, [allTracks, currentTrack, playTrack]); |
| 66 | + |
| 67 | + const playPrev = useCallback(() => { |
| 68 | + if (!currentTrack) return; |
| 69 | + const currentIndex = allTracks.findIndex(t => t.id === currentTrack.id); |
| 70 | + const prevIndex = (currentIndex - 1 + allTracks.length) % allTracks.length; |
| 71 | + playTrack(allTracks[prevIndex]); |
| 72 | + }, [allTracks, currentTrack, playTrack]); |
| 73 | + |
35 | 74 | // Initialize Audio |
36 | 75 | useEffect(() => { |
37 | 76 | audioRef.current = new Audio(); |
@@ -64,46 +103,40 @@ export function AudioProvider({ children, allTracks }: { children: React.ReactNo |
64 | 103 | audioRef.current?.removeEventListener('playing', handlePlaying); |
65 | 104 | audioRef.current?.removeEventListener('loadstart', handleLoadStart); |
66 | 105 | }; |
67 | | - }, []); |
| 106 | + }, [playNext]); |
68 | 107 |
|
69 | | - const playTrack = (track: Track) => { |
70 | | - if (audioRef.current) { |
71 | | - setIsLoading(true); // Set loading immediately |
72 | | - audioRef.current.src = track.url; |
73 | | - audioRef.current.play().catch(e => { |
74 | | - console.error("Playback failed:", e); |
75 | | - setIsLoading(false); |
| 108 | + // Media Session Support (Lock Screen Controls) |
| 109 | + useEffect(() => { |
| 110 | + if (typeof window !== 'undefined' && 'mediaSession' in navigator && currentTrack) { |
| 111 | + navigator.mediaSession.metadata = new MediaMetadata({ |
| 112 | + title: currentTrack.title, |
| 113 | + artist: currentTrack.singer && currentTrack.singer !== 'Unknown' ? currentTrack.singer : 'Sai Rahasya', |
| 114 | + album: currentTrack.category, |
| 115 | + artwork: [ |
| 116 | + { src: '/app-icon.png', sizes: '512x512', type: 'image/png' }, |
| 117 | + ] |
76 | 118 | }); |
77 | | - setCurrentTrack(track); |
78 | | - setIsPlaying(true); |
79 | | - } |
80 | | - }; |
81 | 119 |
|
82 | | - const togglePlay = () => { |
83 | | - if (audioRef.current) { |
84 | | - if (isPlaying) { |
85 | | - audioRef.current.pause(); |
86 | | - setIsPlaying(false); |
87 | | - } else { |
88 | | - audioRef.current.play().catch(console.error); |
89 | | - setIsPlaying(true); |
90 | | - } |
| 120 | + navigator.mediaSession.setActionHandler('play', togglePlay); |
| 121 | + navigator.mediaSession.setActionHandler('pause', togglePlay); |
| 122 | + navigator.mediaSession.setActionHandler('previoustrack', playPrev); |
| 123 | + navigator.mediaSession.setActionHandler('nexttrack', playNext); |
| 124 | + |
| 125 | + return () => { |
| 126 | + navigator.mediaSession.setActionHandler('play', null); |
| 127 | + navigator.mediaSession.setActionHandler('pause', null); |
| 128 | + navigator.mediaSession.setActionHandler('previoustrack', null); |
| 129 | + navigator.mediaSession.setActionHandler('nexttrack', null); |
| 130 | + }; |
91 | 131 | } |
92 | | - }; |
93 | | - |
94 | | - const playNext = () => { |
95 | | - if (!currentTrack) return; |
96 | | - const currentIndex = allTracks.findIndex(t => t.id === currentTrack.id); |
97 | | - const nextIndex = (currentIndex + 1) % allTracks.length; |
98 | | - playTrack(allTracks[nextIndex]); |
99 | | - }; |
| 132 | + }, [currentTrack, playNext, playPrev, togglePlay]); |
100 | 133 |
|
101 | | - const playPrev = () => { |
102 | | - if (!currentTrack) return; |
103 | | - const currentIndex = allTracks.findIndex(t => t.id === currentTrack.id); |
104 | | - const prevIndex = (currentIndex - 1 + allTracks.length) % allTracks.length; |
105 | | - playTrack(allTracks[prevIndex]); |
106 | | - }; |
| 134 | + // Sync playback state with Media Session |
| 135 | + useEffect(() => { |
| 136 | + if (typeof window !== 'undefined' && 'mediaSession' in navigator) { |
| 137 | + navigator.mediaSession.playbackState = isPlaying ? 'playing' : 'paused'; |
| 138 | + } |
| 139 | + }, [isPlaying]); |
107 | 140 |
|
108 | 141 | const closePlayer = () => { |
109 | 142 | if (audioRef.current) { |
|
0 commit comments