Where does this go?
data-audio="http://hi5.1980s.fm/;"
That goes on each "playButton " element, being changed of course for the different station streams.
Can you add it to this one and show me.
I still donāt understand.
Iāll be glad to. Just please provide some code that doesnāt have horrible āhideaā and āplayaā and pausea" class names first.
By which I mean, please remove that suffix character from the class names.
Can you show me an example of what you mean?
I removed that suffix character from the class names.
Here:
If they all say playButton Iām not going to be able to differentiate them from each other. All the styles are in the css. If I want to change something in one of them, Iām not going to know which goes with which.
The CSS people have good solutions for that, such as by calling one āplayButton thinWideā and another āplayButton squareā
That way you can target styles to .playButton
to affect all of them, or just to .square .play
to affect the square play icon.
Hereās an example of what I mean:
function togglePlayButton(button, audio) {
pauseAllButtons();
if (isPlaying(audio)) {
pausePlayer(audio);
} else {
playButton(button);
playPlayer(audio, button.getAttribute("data-audio"));
}
}
What spot?
Location in the javascript?
(function iife() {
"use strict";
function show(el) {
el.classList.remove("hide");
}
function hide(el) {
el.classList.add("hide");
}
function getPlayButton(el) {
while (el.classList.contains("playButton") === false) {
el = el.parentNode;
}
return el;
}
function togglePlayButton(button) {
var player = button.querySelector("audio");
var play = button.querySelector(".play");
var pause = button.querySelector(".pause");
player.volume = 1.0;
if (player.paused) {
hide(play);
show(pause);
player.play();
button.classList.add("active");
} else {
button.classList.remove("active");
show(play);
hide(pause);
player.pause();
}
}
function playButtonClickHandler(evt) {
var button = getPlayButton(evt.target);
togglePlayButton(button);
}
var playButton = document.querySelector(".playButton");
playButton.addEventListener("click", playButtonClickHandler);
}());
Itās called the togglePlayButton() function, so it would replace that one.
Other functions are required of course to make things work, but we can progress towards that.
Do I add this to every audio player?
Iām way confused.
Can you show me a working example?
So I know how to implement it.
What did I do wrong?
function togglePlayButton(button, audio) {
var player = button.querySelector("audio");
var play = button.querySelector(".play");
var pause = button.querySelector(".pause");
player.volume = 1.0;
pauseAllButtons();
if (player.paused) {
hide(play);
show(pause);
player.play();
button.classList.add("active");
} else {
playButton(button);
playPlayer(audio, button.getAttribute("data-audio"));
button.classList.remove("active");
show(play);
hide(pause);
player.pause();
}
}
When you duplicate everything then yes, you have to add it to everything.
Thatās why there are benefits to using only one set of code to control everything.
Would this be the order in which they are placed?
function show(el) {
function hide(el) {
function getPlayButton(el) {
function togglePlayButton(button, audio) {
function playButton(button) {
function playButtonClickHandler(evt) {
var playButton
(function iife() {
"use strict";
function show(el) {
el.classList.remove("hidea");
}
function hide(el) {
el.classList.add("hidea");
}
function getPlayButton(el) {
while (el.classList.contains("playButtona") === false) {
el = el.parentNode;
}
return el;
}
function togglePlayButton(button, audio) {
pauseAllButtons();
if (isPlaying(audio)) {
pausePlayer(audio);
} else {
playButton(button);
playPlayer(audio, button.getAttribute("data-audio"));
}
}
function togglePlayButton(button) {
var player = button.querySelector("audio");
var play = button.querySelector(".playa");
var pause = button.querySelector(".pausea");
player.volume = 1.0;
if (player.paused) {
hide(play);
show(pause);
player.play();
button.classList.add("active");
} else {
button.classList.remove("active");
show(play);
hide(pause);
player.pause();
}
}
function playButtonClickHandler(evt) {
var button = getPlayButton(evt.target);
togglePlayButton(button);
}
var playButton = document.querySelector(".playButtona");
playButton.addEventListener("click", playButtonClickHandler);
}());
Not quite. This will be the order:
show
hide
getButtonContainer
getPlay
getPause
showPlayButton
showPauseButton
pauseAllButtons
getAudio
isPlaying
playAudio
pauseAudio
togglePlayButton
playButtonClickHandler
I donāt have this.
getButtonContainer
Because the original getPlayButton conflicts with showPlayButton and showPauseButton, a better name of getButtonContainer
was chosen for the function that gets the āplayButtonā element that contains the initial/play/pause/speaker elements.
I donāt understand how to set that up. You will have to show me.