-
Notifications
You must be signed in to change notification settings - Fork 2
/
ControlBar.vue
358 lines (310 loc) · 15.5 KB
/
ControlBar.vue
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<script lang="ts" setup>
import { state, getPlaylist, addTrackToPlaylist, setPlaylistIndex } from '~/stores/playlist'
import { watch } from 'vue'
let audioPlayer = ref(null)
const trackData = ref('string')
let mediaData
const bulkTrackData = ref(null)
const isPlaying = ref(true)
const isShuffled = ref(false)
const isLooped = ref(false)
const musicBarWidth = ref(0)
const currentTime = ref(0)
const currentVolume = ref(1)
const playlist = computed({
get: () => state.playlist,
set: value => addTrackToPlaylist(value),
})
const index = computed({
get: () => state.playlistIndex,
set: value => setPlaylistIndex(value),
})
const getTrackData = (trackId: string) => {
fetch('https://audius-discovery-6.cultur3stake.com/v1/tracks/' + trackId + '?app_name=MOODS-TM',
{
method: 'GET',
}).then(response => response.json())
.then(data => {
// Handle the JSON data
trackData.value = data.data;
setupMediaSession(data.data);
return data.data;
})
.catch(error => {
// Handle any errors
console.error(error);
});
}
const getBulkData = () => {
fetch('https://audius-discovery-6.cultur3stake.com/v1/tracks?' + state.playlist.map(item => `id=${item}`).join('&') + '&app_name=MOODS-TM',
{
method: 'GET',
}).then(response => response.json())
.then(data => {
// Handle the JSON data
bulkTrackData.value = data.data;
})
.catch(error => {
// Handle any errors
console.error(error);
});
}
watch(index, (newValue, oldValue) => {
console.log("Index updated", newValue, oldValue)
audioPlayer = new Audio('https://audius-discovery-4.theblueprint.xyz/v1/tracks/' + state.playlist[state.playlistIndex] + '/stream');
audioPlayer.addEventListener('ended', handleNextTrack);
audioPlayer.addEventListener('timeupdate', updateMusicBar)
audioPlayer.volume = currentVolume.value
audioPlayer.play();
getTrackData(state.playlist[state.playlistIndex]);
})
watch(state.playlist, (newValue, oldValue) => {
if (state.playlistIndex === -1) {
setPlaylistIndex(0);
}
getBulkData();
})
const updateMusicBar = () => {
currentTime.value = audioPlayer.currentTime;
musicBarWidth.value = (audioPlayer.currentTime / audioPlayer.duration) * 100
}
const handleNextTrack = () => {
const currentIndex = state.playlistIndex;
const nextIndex = currentIndex + 1;
if (nextIndex < playlist.value.length) {
setPlaylistIndex(nextIndex);
} else {
if (isLooped.value === true) {
if (currentIndex === 0) {
audioPlayer.currentTime = 0;
audioPlayer.play();
console.log('Looping the playlist, but a single track is playing')
} else {
setPlaylistIndex(0);
console.log('Looping the playlist')
}
} else {
console.log('Reached the end of the playlist');
}
}
}
const playPauseButton = () => {
if (audioPlayer.paused) {
audioPlayer.play();
} else {
audioPlayer.pause();
}
isPlaying.value = !isPlaying.value;
}
const shuffleButton = () => {
isShuffled.value = !isShuffled.value;
}
const loopButton = () => {
isLooped.value = !isLooped.value;
}
const skipTrack = () => {
if (state.playlistIndex + 1 < playlist.value.length) {
audioPlayer.pause();
setPlaylistIndex(state.playlistIndex + 1);
}
}
const redoTrack = () => {
if (state.playlistIndex > 0) {
audioPlayer.pause();
setPlaylistIndex(state.playlistIndex - 1);
}
}
const updateCurrentVolume = () => {
audioPlayer.volume = currentVolume.value
}
function setupMediaSession(superTrackData) {
if (navigator.mediaSession) {
navigator.mediaSession.metadata = new MediaMetadata({
title: superTrackData.title,
artist: superTrackData.user.name + ' on MOOD™',
artwork: [
{ src: superTrackData.artwork['480x480'], sizes: '96x96', type: 'image/png' },
{ src: superTrackData.artwork['480x480'], sizes: '256x256', type: 'image/png' }
]
})
navigator.mediaSession.setActionHandler('play', () => {
audioPlayer.play()
})
navigator.mediaSession.setActionHandler('pause', () => {
audioPlayer.pause()
})
navigator.mediaSession.setActionHandler('previoustrack', () => {
redoTrack()
})
navigator.mediaSession.setActionHandler('nexttrack', () => {
skipTrack()
})
}
}
</script>
<template>
<div v-if="state.playlistIndex != -1">
<div
class="fixed bottom-0 left-0 z-50 grid w-full h-24 grid-cols-1 px-4 bg-base-300 border-t-2 border-primary-content md:grid-cols-3">
<div class="items-center justify-center hidden me-auto md:flex">
<img class="h-12 me-3 rounded" :src="trackData.artwork['480x480']" alt="Song's Artwork">
<div class="inline-block leading-none">
<span class="text-sm">
<NuxtLink :to="'/track/' + trackData.id">
<Icon name="ph:music-note-fill" /> {{ trackData.title }}
</NuxtLink>
</span>
<br>
<span class="text-sm">
<NuxtLink :to="'/user/' + trackData.user.handle">
<Icon name="ph:person-fill" /> {{ trackData.user.name }}
</NuxtLink>
</span>
</div>
</div>
<div class="flex items-center w-full">
<div class="w-full">
<div class="flex items-center justify-center mx-auto mb-1 self-end">
<button data-tooltip-target="tooltip-expand" type="button" onclick="sound_modal_3.showModal()"
class="p-2.5 mr-8 md:hidden group rounded-full hover:bg-gray-100 me-1 dark:hover:bg-gray-600">
<Icon name="streamline:volume-level-high-solid" class="text-primary" />
<span class="sr-only">Adjust Volume</span>
</button>
<dialog id="sound_modal_1" class="modal">
<div class="absolute modal-box w-5 left-4 bottom-8 p-0 m-0 rounded-sm"
style="height: 136px;">
<input type="range" min="0" max="1" step="0.1" orient="vertical" class="translate-y-1"
style="writing-mode: vertical-lr; direction: rtl" v-model="currentVolume"
@input="updateCurrentVolume();" />
</div>
<form method="dialog" class="modal-backdrop">
<button>close</button>
</form>
</dialog>
<dialog id="sound_modal_3" class="modal">
<div class="absolute bottom-28 modal-box flex items-center gap-2 p-2 rounded-lg w-10/12">
<Icon name="streamline:volume-mute-solid" />
<input type="range" min="0" max="1" step="0.01" class="range" v-model="currentVolume"
@input="updateCurrentVolume();" />
<Icon name="streamline:volume-level-high-solid" />
</div>
<form method="dialog" class="modal-backdrop">
<button>close</button>
</form>
</dialog>
<div class="tooltip" data-tip="Not Implemented">
<button data-tooltip-target="tooltip-shuffle" type="button"
class="p-2.5 group rounded-full me-1">
<Icon name="streamline:play-list-folder-solid" class="text-error" />
<span class="sr-only">Add to Playlist</span>
</button>
</div>
<button @click="redoTrack();" data-tooltip-target="tooltip-previous" type="button"
class="p-2.5 group rounded-full">
<Icon name="streamline:button-previous-solid" class="text-primary" />
<span class="sr-only">Previous Song</span>
</button>
<button @click="playPauseButton();" data-tooltip-target="tooltip-pause" type="button"
class="inline-flex items-center justify-center p-2.5 mx-2 font-medium bg-primary rounded-lg">
<Icon
:name="audioPlayer.paused ? 'streamline:button-play-solid' : 'streamline:button-pause-2-solid'"
class="text-primary-content" />
<span class="sr-only">Pause Song</span>
</button>
<button @click="skipTrack();" data-tooltip-target="tooltip-next" type="button"
class="p-2.5 group rounded-full">
<Icon name="streamline:button-next-solid" class="text-primary" />
<span class="sr-only">Next Song</span>
</button>
<div class="tooltip" data-tip="Not Implemented">
<button data-tooltip-target="tooltip-restart" type="button"
class="p-2.5 group rounded-full ">
<Icon name="streamline:hearts-symbol-solid" class="text-error" />
<span class="sr-only">Add to Favorites</span>
</button>
</div>
<button data-tooltip-target="tooltip-volume" type="button" onclick="queue_modal.showModal()"
class="p-2.5 ml-8 md:hidden group rounded-lg bg-base-100 hover:bg-gray-100 dark:hover:bg-gray-600">
<Icon name="ph:playlist-bold" class="text-primary" />
{{ playlist.length }}
<span class="sr-only">Open Queue</span>
</button>
<dialog id="queue_modal" class="modal">
<div class="modal-box h-96 w-96 relative overflow-y-auto">
<h3 class="font-bold text-lg">My Queue</h3>
<div v-if="state.playlistIndex != -1 ">
<div v-if="bulkTrackData" v-for="track in bulkTrackData">
<SongCardMinimal :trackParsedData=track />
</div>
</div>
<div class="flex justify-center gap-2 z-10 sticky bottom-0">
<button @click="shuffleButton();" data-tooltip-target="tooltip-captions"
type="button"
class="btn w-1/2 inline-block p-0 rounded-lg text-error z-20">
<Icon name="streamline:shuffle-solid" class="text-error" />
Shuffle Tracks
</button>
<button @click="loopButton();" data-tooltip-target="tooltip-captions" type="button"
:class="[isLooped ? 'btn btn-success w-1/2 inline-block p-0 rounded-lg z-20' : 'btn btn-default w-1/2 inline-block p-0 rounded-lg z-20']">
<Icon name="streamline:arrow-infinite-loop-solid"
:class="[isLooped ? 'text-primary' : 'text-secondary']" />
Loop Tracks
</button>
</div>
</div>
<form method="dialog" class="modal-backdrop">
<button>close</button>
</form>
</dialog>
</div>
<div class="flex items-center justify-between space-x-2 rtl:space-x-reverse">
<span class="text-sm font-medium text-gray-500 dark:text-gray-400 inline-flex">
{{ new Date(currentTime * 1000).toISOString().substring(14, 19) }}</span>
<div class="w-full bg-base-100 rounded-full h-1.5">
<div class="bg-primary h-1.5 rounded-full" :style="{ width: musicBarWidth + '%' }">
</div>
</div>
<span class="text-sm font-medium text-gray-500 dark:text-gray-400 inline-flex">
{{ new Date(trackData.duration * 1000).toISOString().substring(14, 19) }}</span>
</div>
</div>
</div>
<div class="items-center justify-center hidden ms-auto md:flex">
<div class="tooltip" data-tip="Not Implemented">
<button data-tooltip-target="tooltip-playlist" type="button"
class="p-2.5 group rounded-full hover:bg-gray-100 me-1 dark:hover:bg-gray-600">
<Icon name="streamline:shuffle-solid" class="text-error" />
<span class="sr-only">Shuffle Tracks</span>
</button>
</div>
<button @click="loopButton();"
class="p-2.5 group rounded-full hover:bg-gray-100 me-1 dark:hover:bg-gray-600">
<Icon name="streamline:arrow-infinite-loop-solid"
:class="[isLooped ? 'text-primary' : 'texttext-secondary']" />
<span class="sr-only">Loop Tracks</span>
</button>
<button onclick="sound_modal_3.showModal()"
class="p-2.5 group rounded-full hover:bg-gray-100 me-1 dark:hover:bg-gray-600">
<Icon name="streamline:volume-level-high-solid" class="text-primary" />
<span class="sr-only">Adjust Volume</span>
</button>
<dialog id="sound_modal_2" class="modal">
<div class="absolute modal-box w-3 right-12 bottom-8 p-0 m-0 rounded-sm" style="height: 136px;">
<input type="range" min="0" max="1" step="0.1" orient="vertical" class="translate-y-1"
style="writing-mode: vertical-lr; direction: rtl" v-model="currentVolume"
@input="updateCurrentVolume();" />
</div>
<form method="dialog" class="modal-backdrop">
<button>close</button>
</form>
</dialog>
<button onclick="queue_modal.showModal()"
class="p-2.5 group rounded-lg bg-base-100 hover:bg-gray-100 dark:hover:bg-gray-600">
<Icon name="ph:playlist-bold" class="text-primary" />
{{ playlist.length }}
<span class="sr-only">Open Queue</span>
</button>
</div>
</div>
</div>
</template>