Skip to content

Commit

Permalink
Update player.js
Browse files Browse the repository at this point in the history
  • Loading branch information
emadadel4 committed Sep 25, 2024
1 parent aa0646e commit c94495d
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions assets/js/player.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
let isPlaying = false;
let currentSoura = null;
const audioPlayer = document.getElementById('audioPlayer');
const souraTitle = document.getElementById('souraTitle');
const button = document.getElementById('playPauseButton');
const icon = button.querySelector('i');


async function loadSoura() {
try {
Expand Down Expand Up @@ -30,8 +35,6 @@ function playSoura(data) {
const dayOfMonth = today.getDate();
const startIndex = (dayOfMonth - 1) * 3;

const audioPlayer = document.getElementById('audioPlayer');
const souraTitle = document.getElementById('souraTitle');
let currentIndex = 0;

const playCurrentSoura = () => {
Expand All @@ -50,8 +53,7 @@ function playSoura(data) {
const savedTime = localStorage.getItem(`audioTime_${selectedSoura.url}`);
audioPlayer.currentTime = savedTime ? parseFloat(savedTime) : 0;

// Set the text of the soura title element
souraTitle.textContent = `الان: ${selectedSoura.name}`;
souraTitle.textContent = `سور اليوم`;

// Play the audio
audioPlayer.play();
Expand All @@ -63,6 +65,13 @@ function playSoura(data) {

audioPlayer.addEventListener('timeupdate', timeUpdateListener);


audioPlayer.onplay = () => {
souraTitle.textContent = `الان - ${selectedSoura.name}`;
button.classList.replace('play', 'pause');
icon.classList.replace('fa-play', 'fa-pause');
};

// Play the next soura once the current one ends
audioPlayer.onended = () => {
// Remove the timeupdate listener to avoid memory leaks
Expand All @@ -73,7 +82,8 @@ function playSoura(data) {
if (currentIndex < 3) {
playCurrentSoura();
} else {
souraTitle.textContent = 'لقد استمعت الثالث سور اليوم';
localStorage.clear(); // Clear all storage after all souras have been played
location.reload(); // Refresh the page
}
};
} else {
Expand All @@ -87,9 +97,7 @@ function playSoura(data) {


function togglePlayPause() {
const button = document.getElementById('playPauseButton');
const audioPlayer = document.getElementById('audioPlayer');
const icon = button.querySelector('i');


if (isPlaying) {
audioPlayer.pause();
Expand All @@ -102,6 +110,7 @@ function togglePlayPause() {
}

isPlaying = !isPlaying;

}

loadSoura();
loadSoura();

0 comments on commit c94495d

Please sign in to comment.