Skip to content

Commit

Permalink
feat: Fixed Music Button issue #68 (#69)
Browse files Browse the repository at this point in the history
Co-authored-by: neev jewalkar <neevjewalkar@neevs-MacBook-Pro.local>
Co-authored-by: Restyled.io <commits@restyled.io>
  • Loading branch information
3 people authored Jul 31, 2023
1 parent cad584d commit f2bcb2c
Show file tree
Hide file tree
Showing 24 changed files with 146 additions and 110 deletions.
101 changes: 61 additions & 40 deletions js/christmas-songs.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,67 @@
var songs = [
"/music/christmas/AVeryMogulChristmas.mp3",
"/music/christmas/FelizNavidad.mp3",
"/music/christmas/DeckTheHalls.mp3",
"/music/christmas/It'sThatTimeOfTheYear.mp3",
];

var audioElement = document.getElementById('my_audio');
var musicOnButton = document.getElementById('musicOnButton');

function playMusic() {
var randomIndex = Math.floor(Math.random() * songs.length);
var audioSource = document.getElementById('audio_source');
audioSource.src = songs[randomIndex];
audioElement.load();

audioElement.addEventListener('canplaythrough', function() {
audioElement.play().catch(function(error) {
console.log('Error playing audio:', error);
// Song URL
const songs = [
"/music/christmas/AVeryMogulChristmas.mp3",
"/music/christmas/FelizNavidad.mp3",
"/music/christmas/DeckTheHalls.mp3",
"/music/christmas/It'sThatTimeOfTheYear.mp3",
];

// Initialize DOM variables
let audioElement = document.getElementById("my_audio");
let musicOnButton = document.getElementById("musicOnButton");
let musicOffButton = document.getElementById("musicOffButton");

// Define the playMusic function, handling the music
const playMusic = () => {
// Define the song and its URL
let randomIndex = Math.floor(Math.random() * songs.length);
let audioSource = document.getElementById("audio_source");

// Load the song info into the audioElement
audioSource.src = songs[randomIndex];
audioElement.load();

// Update the AudioElement to handle playing the song
audioElement.addEventListener(
"canplaythrough",
() => {
try {
audioElement.play();
musicOnButton.style.display = "none";
musicOffButton.style.display = "block";
} catch (err) {
pauseMusic();
});
musicOnButton.style.display = 'none';
}, { once: true });
throw err;
}
},
{ once: true }
);

audioElement.addEventListener('pause', function() {
musicOnButton.style.display = 'block';
});
}
// Update the AudioElement to handle pausing the song
audioElement.addEventListener("pause", function () {
musicOnButton.style.display = "block";
musicOffButton.style.display = "none";
});
};

function pauseMusic() {
audioElement.pause();
musicOnButton.style.display = 'block';
}
// Define the pauseMusic function, handling the commands to stop the music
const pauseMusic = () => {
audioElement.pause();
musicOnButton.style.display = "block";
musicOffButton.style.display = "none";
};

function toggleMusic() {
if (audioElement.paused) {
playMusic();
} else {
pauseMusic();
}
// Define the toggleMusic function, handling the commands to toggle
// the buttons "musicOnButton" and "musicOffButton"
const toggleMusic = () => {
if (audioElement.paused) {
playMusic();
} else {
pauseMusic();
}
};

// Play music on page load
window.addEventListener('load', function() {
playMusic();
});
// Play music on page load
window.addEventListener("load", () => {
playMusic();
});
5 changes: 2 additions & 3 deletions js/rules.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
document.querySelector(".go-back").addEventListener("click", () => {
window.history.back();
});

window.history.back();
});
94 changes: 51 additions & 43 deletions js/songs.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,61 @@
var songs = [
"/music/apc.mp3",
"/music/beachvibes.mp3",
"/music/bliss.mp3",
"/music/Fluffing-a-Duck.mp3",
"/music/happyafricanvillage.mp3",
"/music/happyandjoyfulchildren.mp3",
"/music/newlands.mp3",
"/music/sotb.mp3",
"/music/tropicalfever.mp3",
"/music/tropicalsoul.mp3",
"/music/ukulele.mp3",
];
const songs = [
"/music/apc.mp3",
"/music/beachvibes.mp3",
"/music/bliss.mp3",
"/music/Fluffing-a-Duck.mp3",
"/music/happyafricanvillage.mp3",
"/music/happyandjoyfulchildren.mp3",
"/music/newlands.mp3",
"/music/sotb.mp3",
"/music/tropicalfever.mp3",
"/music/tropicalsoul.mp3",
"/music/ukulele.mp3",
];

var audioElement = document.getElementById('my_audio');
var musicOnButton = document.getElementById('musicOnButton');
let audioElement = document.getElementById("my_audio");
let musicOnButton = document.getElementById("musicOnButton");
let musicOffButton = document.getElementById("musicOffButton");

function playMusic() {
var randomIndex = Math.floor(Math.random() * songs.length);
var audioSource = document.getElementById('audio_source');
audioSource.src = songs[randomIndex];
audioElement.load();
const playMusic = () => {
var randomIndex = Math.floor(Math.random() * songs.length);
var audioSource = document.getElementById("audio_source");
audioSource.src = songs[randomIndex];
audioElement.load();

audioElement.addEventListener('canplaythrough', function() {
audioElement.play().catch(function(error) {
console.log('Error playing audio:', error);
audioElement.addEventListener(
"canplaythrough",
() => {
audioElement.play().catch((error) => {
console.log("Error playing audio:", error);
pauseMusic();
});
musicOnButton.style.display = 'none';
}, { once: true });
musicOnButton.style.display = "none";
musicOffButton.style.display = "block";
},
{ once: true }
);

audioElement.addEventListener('pause', function() {
musicOnButton.style.display = 'block';
});
}
audioElement.addEventListener("pause", () => {
musicOnButton.style.display = "block";
musicOffButton.style.display = "none";
});
};

function pauseMusic() {
audioElement.pause();
musicOnButton.style.display = 'block';
}
const pauseMusic = () => {
audioElement.pause();
musicOnButton.style.display = "block";
musicOffButton.style.display = "none";
};

function toggleMusic() {
if (audioElement.paused) {
playMusic();
} else {
pauseMusic();
}
const toggleMusic = () => {
if (audioElement.paused) {
playMusic();
} else {
pauseMusic();
}
};

// Play music on page load
window.addEventListener('load', function() {
playMusic();
});
// Play music on page load
window.addEventListener("load", () => {
playMusic();
});
4 changes: 2 additions & 2 deletions js/topics.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
function toggleHidde(containerId) {
const toggleHidde = (containerId) => {
// removes one & adds the other
document.getElementById(containerId).classList.toggle("hide");
document.getElementById(containerId).classList.toggle("show");
}
};

document
.getElementById("technology")
Expand Down
12 changes: 10 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pages/browsers/game.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ <h1 id="question">What is the answer to this question</h1>

<div class="music-container flex-center flex-row">
<a onclick="toggleMusic()" class="button1" id="musicOnButton" style="display: none;"> Music On <i class="fas fa-light fa-headphones"></i></a>
<a onclick="pauseMusic()" class="button2"> Music Off <i class="fas fa-light fa-volume-off"></i></a>
<a onclick="pauseMusic()" class="button2" id="musicOffButton"> Music Off <i class="fas fa-light fa-volume-off"></i></a>
</div>
</div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion pages/christmas/game.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ <h1 id="question">What is the answer to this question</h1>

<div class="music-container flex-center flex-row">
<a onclick="toggleMusic()" class="button1" id="musicOnButton" style="display: none;"> Music On <i class="fas fa-light fa-headphones"></i></a>
<a onclick="pauseMusic()" class="button2"> Music Off <i class="fas fa-light fa-volume-off"></i></a>
<a onclick="pauseMusic()" class="button2" id="musicOffButton"> Music Off <i class="fas fa-light fa-volume-off"></i></a>
</div>
</div>
</body>
Expand Down
4 changes: 2 additions & 2 deletions pages/computer_parts/game.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ <h1 id="question">What is the answer to this question</h1>
<audio loop="loop" id="my_audio" preload="auto" autoplay>
<source id="audio_source" src="" type="audio/mpeg">
</audio>

<div class="music-container flex-center flex-row">
<a onclick="toggleMusic()" class="button1" id="musicOnButton" style="display: none;"> Music On <i class="fas fa-light fa-headphones"></i></a>
<a onclick="pauseMusic()" class="button2"> Music Off <i class="fas fa-light fa-volume-off"></i></a>
<a onclick="pauseMusic()" class="button2" id="musicOffButton"> Music Off <i class="fas fa-light fa-volume-off"></i></a>
</div>
</div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion pages/countries/game.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ <h1 id="question">What is the answer to this question</h1>

<div class="music-container flex-center flex-row">
<a onclick="toggleMusic()" class="button1" id="musicOnButton" style="display: none;"> Music On <i class="fas fa-light fa-headphones"></i></a>
<a onclick="pauseMusic()" class="button2"> Music Off <i class="fas fa-light fa-volume-off"></i></a>
<a onclick="pauseMusic()" class="button2" id="musicOffButton"> Music Off <i class="fas fa-light fa-volume-off"></i></a>
</div>
</div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion pages/disney/game.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ <h1 id="question">What is the answer to this question</h1>

<div class="music-container flex-center flex-row">
<a onclick="toggleMusic()" class="button1" id="musicOnButton" style="display: none;"> Music On <i class="fas fa-light fa-headphones"></i></a>
<a onclick="pauseMusic()" class="button2"> Music Off <i class="fas fa-light fa-volume-off"></i></a>
<a onclick="pauseMusic()" class="button2" id="musicOffButton"> Music Off <i class="fas fa-light fa-volume-off"></i></a>
</div>
</div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion pages/easter/game.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ <h1 id="question">What is the answer to this question</h1>

<div class="music-container flex-center flex-row">
<a onclick="toggleMusic()" class="button1" id="musicOnButton" style="display: none;"> Music On <i class="fas fa-light fa-headphones"></i></a>
<a onclick="pauseMusic()" class="button2"> Music Off <i class="fas fa-light fa-volume-off"></i></a>
<a onclick="pauseMusic()" class="button2" id="musicOffButton"> Music Off <i class="fas fa-light fa-volume-off"></i></a>
</div>
</div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion pages/football/game.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ <h1 id="question">What is the answer to this question</h1>

<div class="music-container flex-center flex-row">
<a onclick="toggleMusic()" class="button1" id="musicOnButton" style="display: none;"> Music On <i class="fas fa-light fa-headphones"></i></a>
<a onclick="pauseMusic()" class="button2"> Music Off <i class="fas fa-light fa-volume-off"></i></a>
<a onclick="pauseMusic()" class="button2" id="musicOffButton"> Music Off <i class="fas fa-light fa-volume-off"></i></a>
</div>
</div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion pages/fruit/game.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ <h1 id="question">What is the answer to this question</h1>

<div class="music-container flex-center flex-row">
<a onclick="toggleMusic()" class="button1" id="musicOnButton" style="display: none;"> Music On <i class="fas fa-light fa-headphones"></i></a>
<a onclick="pauseMusic()" class="button2"> Music Off <i class="fas fa-light fa-volume-off"></i></a>
<a onclick="pauseMusic()" class="button2" id="musicOffButton"> Music Off <i class="fas fa-light fa-volume-off"></i></a>
</div>
</div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion pages/game.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ <h1 id="question">What is the answer to this question</h1>

<div class="music-container flex-center flex-row">
<a onclick="toggleMusic()" class="button1" id="musicOnButton" style="display: none;"> Music On <i class="fas fa-light fa-headphones"></i></a>
<a onclick="pauseMusic()" class="button2"> Music Off <i class="fas fa-light fa-volume-off"></i></a>
<a onclick="pauseMusic()" class="button2" id="musicOffButton"> Music Off <i class="fas fa-light fa-volume-off"></i></a>
</div>
</div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion pages/general_knowledge/game.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ <h1 id="question">What is the answer to this question</h1>

<div class="music-container flex-center flex-row">
<a onclick="toggleMusic()" class="button1" id="musicOnButton" style="display: none;"> Music On <i class="fas fa-light fa-headphones"></i></a>
<a onclick="pauseMusic()" class="button2"> Music Off <i class="fas fa-light fa-volume-off"></i></a>
<a onclick="pauseMusic()" class="button2" id="musicOffButton"> Music Off <i class="fas fa-light fa-volume-off"></i></a>
</div>
</div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion pages/jaidenanimations/game.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ <h1 id="question">What is the answer to this question</h1>

<div class="music-container flex-center flex-row">
<a onclick="toggleMusic()" class="button1" id="musicOnButton" style="display: none;"> Music On <i class="fas fa-light fa-headphones"></i></a>
<a onclick="pauseMusic()" class="button2"> Music Off <i class="fas fa-light fa-volume-off"></i></a>
<a onclick="pauseMusic()" class="button2" id="musicOffButton"> Music Off <i class="fas fa-light fa-volume-off"></i></a>
</div>
</div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion pages/kendall-quiz/game.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ <h1 id="question">What is the answer to this question</h1>

<div class="music-container flex-center flex-row">
<a onclick="toggleMusic()" class="button1" id="musicOnButton" style="display: none;"> Music On <i class="fas fa-light fa-headphones"></i></a>
<a onclick="pauseMusic()" class="button2"> Music Off <i class="fas fa-light fa-volume-off"></i></a>
<a onclick="pauseMusic()" class="button2" id="musicOffButton"> Music Off <i class="fas fa-light fa-volume-off"></i></a>
</div>
</div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion pages/minecraft/game.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ <h1 id="question">What is the answer to this question</h1>

<div class="music-container flex-center flex-row">
<a onclick="toggleMusic()" class="button1" id="musicOnButton" style="display: none;"> Music On <i class="fas fa-light fa-headphones"></i></a>
<a onclick="pauseMusic()" class="button2"> Music Off <i class="fas fa-light fa-volume-off"></i></a>
<a onclick="pauseMusic()" class="button2" id="musicOffButton"> Music Off <i class="fas fa-light fa-volume-off"></i></a>
</div>
</div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion pages/presidents/game.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ <h1 id="question">What is the answer to this question</h1>

<div class="music-container flex-center flex-row">
<a onclick="toggleMusic()" class="button1" id="musicOnButton" style="display: none;"> Music On <i class="fas fa-light fa-headphones"></i></a>
<a onclick="pauseMusic()" class="button2"> Music Off <i class="fas fa-light fa-volume-off"></i></a>
<a onclick="pauseMusic()" class="button2" id="musicOffButton"> Music Off <i class="fas fa-light fa-volume-off"></i></a>
</div>
</div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion pages/programming_language/game.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ <h1 id="question">What is the answer to this question</h1>

<div class="music-container flex-center flex-row">
<a onclick="toggleMusic()" class="button1" id="musicOnButton" style="display: none;"> Music On <i class="fas fa-light fa-headphones"></i></a>
<a onclick="pauseMusic()" class="button2"> Music Off <i class="fas fa-light fa-volume-off"></i></a>
<a onclick="pauseMusic()" class="button2" id="musicOffButton"> Music Off <i class="fas fa-light fa-volume-off"></i></a>
</div>
</div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion pages/roblox/game.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ <h1 id="question">What is the answer to this question</h1>

<div class="music-container flex-center flex-row">
<a onclick="toggleMusic()" class="button1" id="musicOnButton" style="display: none;"> Music On <i class="fas fa-light fa-headphones"></i></a>
<a onclick="pauseMusic()" class="button2"> Music Off <i class="fas fa-light fa-volume-off"></i></a>
<a onclick="pauseMusic()" class="button2" id="musicOffButton"> Music Off <i class="fas fa-light fa-volume-off"></i></a>
</div>
</div>
</body>
Expand Down
Loading

0 comments on commit f2bcb2c

Please sign in to comment.