Skip to content

Commit e66c3eb

Browse files
committed
saving and restoring current song
1 parent 58868a6 commit e66c3eb

1 file changed

Lines changed: 20 additions & 5 deletions

File tree

src/main.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,16 @@ function assingMediaSession(song) {
7070
async function playSong(song) {
7171
const { file } = song;
7272
currentSong = song;
73+
saveOption('currentSongId', currentSong.id);
7374
points.setUniform('showMessage', 0);
74-
const audioUrl = URL.createObjectURL(file);
7575
const name = song?.name || file.name;
7676
const title = name;
7777

78+
const audioUrl = URL.createObjectURL(song.file);
7879
audio && audio.pause() && (audio = null);
7980
audio = points.setAudio('audio', audioUrl, options.volume, false, false);
81+
8082
points.setUniform('numChars', title.length < MAXCHARS ? title.length : MAXCHARS);
81-
// points.setStorageMap('chars', strToCodes(title));
8283

8384
const songNameImg = strToImage(title, atlas, size);
8485
await points.setTextureImage('songName', songNameImg);
@@ -305,6 +306,10 @@ if (volume) {
305306
const scheme = await getOption('scheme');
306307
console.log(scheme);
307308

309+
const currentSongId = await getOption('currentSongId')
310+
console.log(currentSongId);
311+
312+
308313
if (scheme) {
309314
selectedScheme['Color Scheme'] = scheme;
310315
points.setUniform('colorScheme', scheme)
@@ -406,8 +411,18 @@ points.canvas.addEventListener('click', _ => {
406411
if (pauseClickTimeout) {
407412
return;
408413
}
409-
pauseClickTimeout = setTimeout(() => {
410-
audio?.paused ? audio?.play() : audio?.pause();
414+
pauseClickTimeout = setTimeout(_ => {
415+
// if the app starts first time it has no audio,
416+
// second time it has the current song saved,
417+
// no src means no song
418+
// if currentSongId then a value was saved and can be loaded
419+
const src = audio.getAttribute('src');
420+
if (!src && currentSongId) {
421+
currentSong = songs[currentSongId];
422+
playSong(currentSong)
423+
} else {
424+
audio?.paused ? audio?.play() : audio?.pause();
425+
}
411426
pauseClickTimeout = null;
412427
}, 300);
413428
});
@@ -490,7 +505,7 @@ loadSongFromURL()
490505

491506
// ----------------------------------
492507

493-
function loadNextSong(){
508+
function loadNextSong() {
494509
const id = +currentSong.id;
495510
const nextSong = songs[id + 1] || songs[0];
496511
playSong(nextSong);

0 commit comments

Comments
 (0)