From d1dad93a82d01e305112392330317bedcd990e0f Mon Sep 17 00:00:00 2001 From: Utkarsh Garg Date: Mon, 9 Oct 2023 18:04:58 +0530 Subject: [PATCH 1/2] play/pause icon working --- script.js | 72 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 45 insertions(+), 27 deletions(-) diff --git a/script.js b/script.js index df50081..db31e78 100644 --- a/script.js +++ b/script.js @@ -339,36 +339,54 @@ let index = 0; let poster_master_play = document.getElementById('poster_master_play'); let tite = document.getElementById('title'); let download_music = document.getElementById('download_music'); +let isPlaying=false; +let pausedTime = 0; // Initialize to 0 Array.from(document.getElementsByClassName('playlistplay')).forEach((e)=>{ e.addEventListener('click', (el)=>{ - index = el.target.id; - // console.log(index); - music.src = `audio/${index}.mp3`; - poster_master_play.src=`img/${index}.jpg`; - music.play(); - wave.classList.add('active2') - masterplay.classList.remove('bi-play-fill') - masterplay.classList.add('bi-pause-fill') - // download - download_music.href =`audio/${index}.mp3`; - - let songTitles = songs.filter((ele)=>{ - return ele.id == index; - }); - - songTitles.forEach(elem =>{ - let {songName} = elem; - title.innerHTML= songName; - download_music.setAttribute('download',songName); - }); - - makeAllBackground(); - Array.from(document.getElementsByClassName('songItem'))[index-1].style.background="rgb(105, 105, 170, .1)"; - - makeAllPlay(); - el.target.classList.remove('bi-play-circle-fill'); - el.target.classList.add('bi-pause-circle-fill'); + //If paused then play and if playing then pause through icon + + if (isPlaying ){ + + music.pause(); + pausedTime=music.currentTime; + wave.classList.remove('active2'); + masterplay.classList.add('bi-play-fill'); + masterplay.classList.remove('bi-pause-fill'); + makeAllPlay(); + } + else{ + index = el.target.id; + // console.log(pauseTime); + music.src = `audio/${index}.mp3`; + poster_master_play.src=`img/${index}.jpg`; + // download + download_music.href =`audio/${index}.mp3`; + let songTitles = songs.filter((ele)=>{ + return ele.id == index; + }); + + songTitles.forEach(elem =>{ + let {songName} = elem; + title.innerHTML= songName; + download_music.setAttribute('download',songName); + }); + + makeAllBackground(); + Array.from(document.getElementsByClassName('songItem'))[index-1].style.background="rgb(105, 105, 170, .1)"; + + music.currentTime=pausedTime; //Initialize time with paused time. + + music.play(); + wave.classList.add('active2') + masterplay.classList.remove('bi-play-fill') + masterplay.classList.add('bi-pause-fill') + + makeAllPlay(); + el.target.classList.remove('bi-play-circle-fill'); + el.target.classList.add('bi-pause-circle-fill'); + } + isPlaying=!isPlaying; }) }) From 16918d547c7792ba5b83f57f66cf7a67ef2ae445 Mon Sep 17 00:00:00 2001 From: Utkarsh Garg Date: Mon, 9 Oct 2023 19:34:35 +0530 Subject: [PATCH 2/2] play/pause working --- script.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/script.js b/script.js index db31e78..cbb2741 100644 --- a/script.js +++ b/script.js @@ -341,6 +341,8 @@ let tite = document.getElementById('title'); let download_music = document.getElementById('download_music'); let isPlaying=false; let pausedTime = 0; // Initialize to 0 +let pausedIndex = 0; +let playingIndex = 0; Array.from(document.getElementsByClassName('playlistplay')).forEach((e)=>{ e.addEventListener('click', (el)=>{ @@ -350,6 +352,7 @@ Array.from(document.getElementsByClassName('playlistplay')).forEach((e)=>{ music.pause(); pausedTime=music.currentTime; + pausedIndex=playingIndex; wave.classList.remove('active2'); masterplay.classList.add('bi-play-fill'); masterplay.classList.remove('bi-pause-fill'); @@ -375,9 +378,15 @@ Array.from(document.getElementsByClassName('playlistplay')).forEach((e)=>{ makeAllBackground(); Array.from(document.getElementsByClassName('songItem'))[index-1].style.background="rgb(105, 105, 170, .1)"; - music.currentTime=pausedTime; //Initialize time with paused time. + if(index==pausedIndex){ + music.currentTime=pausedTime; //Initialize time with paused time. + } + else{ + music.currentTime=0; + } music.play(); + playingIndex=index; wave.classList.add('active2') masterplay.classList.remove('bi-play-fill') masterplay.classList.add('bi-pause-fill')