|
1 | 1 | 'use client'; |
2 | 2 |
|
3 | | -import React, { createContext, useContext, useState, useRef, useEffect, useCallback } from 'react'; |
| 3 | +import React, { createContext, useContext, useState, useRef, useEffect } from 'react'; |
4 | 4 |
|
5 | 5 | export interface Track { |
6 | 6 | id: number; |
@@ -32,45 +32,6 @@ 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 | | - |
74 | 35 | // Initialize Audio |
75 | 36 | useEffect(() => { |
76 | 37 | audioRef.current = new Audio(); |
@@ -103,44 +64,51 @@ export function AudioProvider({ children, allTracks }: { children: React.ReactNo |
103 | 64 | audioRef.current?.removeEventListener('playing', handlePlaying); |
104 | 65 | audioRef.current?.removeEventListener('loadstart', handleLoadStart); |
105 | 66 | }; |
106 | | - }, [playNext]); |
| 67 | + }, []); |
107 | 68 |
|
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 | | - ] |
| 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); |
118 | 76 | }); |
119 | | - |
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 | | - }; |
| 77 | + setCurrentTrack(track); |
| 78 | + setIsPlaying(true); |
131 | 79 | } |
132 | | - }, [currentTrack, playNext, playPrev, togglePlay]); |
| 80 | + }; |
133 | 81 |
|
134 | | - // Sync playback state with Media Session |
135 | | - useEffect(() => { |
136 | | - if (typeof window !== 'undefined' && 'mediaSession' in navigator) { |
137 | | - navigator.mediaSession.playbackState = isPlaying ? 'playing' : 'paused'; |
| 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 | + } |
138 | 91 | } |
139 | | - }, [isPlaying]); |
| 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 | + }; |
| 100 | + |
| 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 | + }; |
140 | 107 |
|
141 | 108 | const closePlayer = () => { |
142 | 109 | if (audioRef.current) { |
143 | 110 | audioRef.current.pause(); |
| 111 | + audioRef.current.src = ''; |
144 | 112 | audioRef.current.currentTime = 0; |
145 | 113 | } |
146 | 114 | setIsPlaying(false); |
|
0 commit comments