Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Fix code: sound on/off in IOS

Nicoch

New Coder
Hello everyone, I'm a beginner in this, but I've been working on this code for a while, which shows images randomly and with different speeds. For each image, a sound is played.

The problem I'm having is that when I play the code on some cell phones (usually iPhones), I'm forced to put the phone in ringer mode, and yet sometimes the sound doesn't play. I would like the sound to play even when in silent mode, with prior authorization from the user, as you can see:
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
body {
font-family: Arial, sans-serif;
}
#imagen {
display: block;
margin: 2px auto 0;
max-width: 100%;
margin-top: 2px;
height: auto;
}
.btn {
font-size: 16px;
padding: 10px 20px;
border-radius: 5px;
border: none;
background-color: #4CAF50;
color: white;
cursor: pointer;
margin-right: 10px;
}
.btn:hover {
background-color: #45a049;
}
select {
font-size: 16px;
padding: 8px;
border-radius: 5px;
border: 2px solid #ccc;
margin-right: 10px;
}
.label-dropdown {
display: inline-block;
margin-right: 5px;
}
#configuracion {
text-align: center;
margin-bottom: 0px;
}
#creditos {
text-align: center;
margin-top: 2px;
color: #666;
}
.mute-icon {
display: inline-block;
width: 20px;
height: 20px;
cursor: pointer;
}
</style>
</head>
<body>
<div id="configuracion">
<select id="intervalo" onchange="cambiarIntervalo()">
<option disabled selected hidden>Speed interval</option>
...
</select>
<select id="cantidad" onchange="cambiarCantidad()">
<option disabled selected hidden>Number of Pads</option>
...
</select>
<button class="btn" id="startStopBtn" onclick="startStopRotation()">Start</button>
<img src="https://nacademysports.com/wp-content/uploads/2023/06/mute.png" id="muteIcon" class="mute-icon" onclick="toggleSound()">
</div>

<img src="https://nacademysports.com/wp-content/uploads/2023/06/PP.png" id="imagen" width="400" height="400">

<script>
var imagenes = [
...
];

var sonidos = [
...
];

var intervaloID; // variable global para almacenar el ID del intervalo
var rotationActive = false; // estado de la rotación (inicialmente detenida)
var cantidad = 8; // cantidad de imágenes a mostrar (valor por defecto)
var soundEnabled = false; // estado del sonido (inicialmente desactivado)

var context = new (window.AudioContext || window.webkitAudioContext)(); // instancia del contexto de audio
var gainNode = context.createGain(); // nodo de ganancia de audio

function rotarImagenes() {
var index = Math.floor(Math.random() * cantidad); // obtener un índice aleatorio
document.getElementById("imagen").src = imagenes[index]; // cambiar la imagen

if (soundEnabled) {
playSound(index); // reproducir el sonido correspondiente
}
}

async function playSound(index) {
try {
const response = await fetch(sonidos[index]);
const arrayBuffer = await response.arrayBuffer();
const audioBuffer = await context.decodeAudioData(arrayBuffer);

const source = context.createBufferSource();
source.buffer = audioBuffer;
source.connect(gainNode);
gainNode.connect(context.destination);
source.start(0);
} catch (error) {
console.error('Error loading or playing audio:', error);
}
}

function cambiarIntervalo() {
var intervalo = document.getElementById("intervalo").value;
clearInterval(intervaloID); // limpiar el intervalo anterior
if (rotationActive) {
intervaloID = setInterval(rotarImagenes, intervalo); // establecer el nuevo intervalo
}
}

function cambiarCantidad() {
cantidad = parseInt(document.getElementById("cantidad").value);
clearInterval(intervaloID); // limpiar el intervalo anterior
if (rotationActive) {
var intervalo = document.getElementById("intervalo").value;
intervaloID = setInterval(rotarImagenes, intervalo); // establecer el nuevo intervalo
}
}

function startStopRotation() {
if (!rotationActive) {
rotationActive = true;
document.getElementById("startStopBtn").textContent = "Stop";
var intervalo = document.getElementById("intervalo").value;
intervaloID = setInterval(rotarImagenes, intervalo);
} else {
rotationActive = false;
document.getElementById("startStopBtn").textContent = "Start";
clearInterval(intervaloID);
}
}

function toggleSound() {
soundEnabled = !soundEnabled;
var muteIcon = document.getElementById("muteIcon");
if (soundEnabled) {
muteIcon.src = "https://nacademysports.com/wp-content/uploads/2023/06/unmute.png";
} else {
muteIcon.src = "https://nacademysports.com/wp-content/uploads/2023/06/mute.png";
}
}
</script>
</body>
</html>

I would like to know if someone with more experience can help me with this, and if you have any suggestions, I'd really appreciate it. Thank you so much.
 
Hello everyone, I'm a beginner in this, but I've been working on this code for a while, which shows images randomly and with different speeds. For each image, a sound is played.

The problem I'm having is that when I play the code on some cell phones (usually iPhones), I'm forced to put the phone in ringer mode, and yet sometimes the sound doesn't play. I would like the sound to play even when in silent mode, with prior authorization from the user, as you can see:
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
body {
font-family: Arial, sans-serif;
}
#imagen {
display: block;
margin: 2px auto 0;
max-width: 100%;
margin-top: 2px;
height: auto;
}
.btn {
font-size: 16px;
padding: 10px 20px;
border-radius: 5px;
border: none;
background-color: #4CAF50;
color: white;
cursor: pointer;
margin-right: 10px;
}
.btn:hover {
background-color: #45a049;
}
select {
font-size: 16px;
padding: 8px;
border-radius: 5px;
border: 2px solid #ccc;
margin-right: 10px;
}
.label-dropdown {
display: inline-block;
margin-right: 5px;
}
#configuracion {
text-align: center;
margin-bottom: 0px;
}
#creditos {
text-align: center;
margin-top: 2px;
color: #666;
}
.mute-icon {
display: inline-block;
width: 20px;
height: 20px;
cursor: pointer;
}
</style>
</head>
<body>
<div id="configuracion">
<select id="intervalo" onchange="cambiarIntervalo()">
<option disabled selected hidden>Speed interval</option>
...
</select>
<select id="cantidad" onchange="cambiarCantidad()">
<option disabled selected hidden>Number of Pads</option>
...
</select>
<button class="btn" id="startStopBtn" onclick="startStopRotation()">Start</button>
<img src="https://nacademysports.com/wp-content/uploads/2023/06/mute.png" id="muteIcon" class="mute-icon" onclick="toggleSound()">
</div>

<img src="https://nacademysports.com/wp-content/uploads/2023/06/PP.png" id="imagen" width="400" height="400">

<script>
var imagenes = [
...
];

var sonidos = [
...
];

var intervaloID; // variable global para almacenar el ID del intervalo
var rotationActive = false; // estado de la rotación (inicialmente detenida)
var cantidad = 8; // cantidad de imágenes a mostrar (valor por defecto)
var soundEnabled = false; // estado del sonido (inicialmente desactivado)

var context = new (window.AudioContext || window.webkitAudioContext)(); // instancia del contexto de audio
var gainNode = context.createGain(); // nodo de ganancia de audio

function rotarImagenes() {
var index = Math.floor(Math.random() * cantidad); // obtener un índice aleatorio
document.getElementById("imagen").src = imagenes[index]; // cambiar la imagen

if (soundEnabled) {
playSound(index); // reproducir el sonido correspondiente
}
}

async function playSound(index) {
try {
const response = await fetch(sonidos[index]);
const arrayBuffer = await response.arrayBuffer();
const audioBuffer = await context.decodeAudioData(arrayBuffer);

const source = context.createBufferSource();
source.buffer = audioBuffer;
source.connect(gainNode);
gainNode.connect(context.destination);
source.start(0);
} catch (error) {
console.error('Error loading or playing audio:', error);
}
}

function cambiarIntervalo() {
var intervalo = document.getElementById("intervalo").value;
clearInterval(intervaloID); // limpiar el intervalo anterior
if (rotationActive) {
intervaloID = setInterval(rotarImagenes, intervalo); // establecer el nuevo intervalo
}
}

function cambiarCantidad() {
cantidad = parseInt(document.getElementById("cantidad").value);
clearInterval(intervaloID); // limpiar el intervalo anterior
if (rotationActive) {
var intervalo = document.getElementById("intervalo").value;
intervaloID = setInterval(rotarImagenes, intervalo); // establecer el nuevo intervalo
}
}

function startStopRotation() {
if (!rotationActive) {
rotationActive = true;
document.getElementById("startStopBtn").textContent = "Stop";
var intervalo = document.getElementById("intervalo").value;
intervaloID = setInterval(rotarImagenes, intervalo);
} else {
rotationActive = false;
document.getElementById("startStopBtn").textContent = "Start";
clearInterval(intervaloID);
}
}

function toggleSound() {
soundEnabled = !soundEnabled;
var muteIcon = document.getElementById("muteIcon");
if (soundEnabled) {
muteIcon.src = "https://nacademysports.com/wp-content/uploads/2023/06/unmute.png";
} else {
muteIcon.src = "https://nacademysports.com/wp-content/uploads/2023/06/mute.png";
}
}
</script>
</body>
</html>

I would like to know if someone with more experience can help me with this, and if you have any suggestions, I'd really appreciate it. Thank you so much.
Hi there,
Espero todo este bien...
Unfortunately I have a bit of bad news for ya... unfortunately, that is a system setting, which takes precedent. There is no way to override silent mode... it's called SILENT MODE for a reason, right? 😉
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom