File size: 6,987 Bytes
d987ee8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Shared JavaScript across all pages
document.addEventListener('DOMContentLoaded', function() {
    // Sample playlist data
    const playlist = [
        {
            id: 1,
            title: "Bohemian Rhapsody",
            artist: "Queen",
            duration: "355",
            url: "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3"
        },
        {
            id: 2,
            title: "Blinding Lights",
            artist: "The Weeknd",
            duration: "200",
            url: "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3"
        },
        {
            id: 3,
            title: "Shape of You",
            artist: "Ed Sheeran",
            duration: "233",
            url: "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-3.mp3"
        },
        {
            id: 4,
            title: "Dance Monkey",
            artist: "Tones and I",
            duration: "209",
            url: "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-4.mp3"
        },
        {
            id: 5,
            title: "Uptown Funk",
            artist: "Mark Ronson ft. Bruno Mars",
            duration: "270",
            url: "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-5.mp3"
        }
    ];

    // DOM Elements
    const playBtn = document.getElementById('play-btn');
    const prevBtn = document.getElementById('prev-btn');
    const nextBtn = document.getElementById('next-btn');
    const playlistContainer = document.getElementById('playlist');
    const trackTitle = document.getElementById('track-title');
    const trackArtist = document.getElementById('track-artist');
    const currentTimeEl = document.getElementById('current-time');
    const totalTimeEl = document.getElementById('total-time');
    const progressBar = document.getElementById('progress-bar');
    const vinyl = document.querySelector('.w-64.h-64');
    const needle = document.getElementById('vinyl-needle');

    // Player state
    let currentTrackIndex = 0;
    let isPlaying = false;
    let sound = null;

    // Initialize playlist
    function initPlaylist() {
        playlistContainer.innerHTML = '';
        playlist.forEach((track, index) => {
            const item = document.createElement('div');
            item.className = `playlist-item p-3 rounded-lg cursor-pointer ${index === currentTrackIndex ? 'active' : ''}`;
            item.innerHTML = `
                <div class="flex justify-between items-center">
                    <div>
                        <div class="font-medium">${track.title}</div>
                        <div class="text-sm text-gray-400">${track.artist}</div>
                    </div>
                    <div class="text-sm text-gray-400">${formatTime(track.duration)}</div>
                </div>
            `;
            item.addEventListener('click', () => {
                playTrack(index);
            });
            playlistContainer.appendChild(item);
        });
    }

    // Play track
    function playTrack(index) {
        // Stop current sound if playing
        if (sound) {
            sound.stop();
        }

        // Update current track index
        currentTrackIndex = index;

        // Update UI
        const track = playlist[currentTrackIndex];
        trackTitle.textContent = track.title;
        trackArtist.textContent = track.artist;
        totalTimeEl.textContent = formatTime(track.duration);
        
        // Update playlist active item
        document.querySelectorAll('.playlist-item').forEach((item, i) => {
            if (i === currentTrackIndex) {
                item.classList.add('active');
            } else {
                item.classList.remove('active');
            }
        });

        // Create new sound
        sound = new Howl({
            src: [track.url],
            html5: true,
            onplay: function() {
                isPlaying = true;
                updatePlayButton();
                rotateVinyl(true);
                moveNeedle(true);
                requestAnimationFrame(updateProgress);
            },
            onpause: function() {
                isPlaying = false;
                updatePlayButton();
                rotateVinyl(false);
                moveNeedle(false);
            },
            onend: function() {
                isPlaying = false;
                updatePlayButton();
                rotateVinyl(false);
                moveNeedle(false);
                nextTrack();
            }
        });

        // Play the sound
        sound.play();
    }

    // Toggle play/pause
    function togglePlay() {
        if (sound) {
            if (isPlaying) {
                sound.pause();
            } else {
                sound.play();
            }
        } else {
            playTrack(currentTrackIndex);
        }
    }

    // Next track
    function nextTrack() {
        currentTrackIndex = (currentTrackIndex + 1) % playlist.length;
        playTrack(currentTrackIndex);
    }

    // Previous track
    function prevTrack() {
        currentTrackIndex = (currentTrackIndex - 1 + playlist.length) % playlist.length;
        playTrack(currentTrackIndex);
    }

    // Update play button icon
    function updatePlayButton() {
        const playIcon = playBtn.querySelector('i');
        if (isPlaying) {
            playIcon.setAttribute('data-feather', 'pause');
        } else {
            playIcon.setAttribute('data-feather', 'play');
        }
        feather.replace();
    }

    // Rotate vinyl
    function rotateVinyl(playing) {
        if (playing) {
            vinyl.classList.add('vinyl-rotation', 'vinyl-playing');
            vinyl.classList.remove('vinyl-paused');
        } else {
            vinyl.classList.remove('vinyl-playing');
            vinyl.classList.add('vinyl-paused');
        }
    }

    // Move needle
    function moveNeedle(playing) {
        if (playing) {
            needle.style.transform = 'rotate(0deg)';
        } else {
            needle.style.transform = 'rotate(-30deg)';
        }
    }

    // Update progress bar
    function updateProgress() {
        if (!sound) return;
        
        const seek = sound.seek() || 0;
        const duration = sound.duration();
        const progress = (seek / duration) * 100;
        
        progressBar.style.width = `${progress}%`;
        currentTimeEl.textContent = formatTime(Math.floor(seek));
        
        if (isPlaying) {
            requestAnimationFrame(updateProgress);
        }
    }

    // Format time (seconds to mm:ss)
    function formatTime(seconds) {
        const mins = Math.floor(seconds / 60);
        const secs = seconds % 60;
        return `${mins}:${secs < 10 ? '0' : ''}${secs}`;
    }

    // Event listeners
    playBtn.addEventListener('click', togglePlay);
    nextBtn.addEventListener('click', nextTrack);
    prevBtn.addEventListener('click', prevTrack);

    // Initialize
    initPlaylist();
    updatePlayButton();
});