Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

206
Visualizações
Audio player. How to add cookies to JS code?

Made from different pieces of code an audio player in JS. He works great. There are two button positions to turn on the sound and turn off the sound. So when you open another page of the site, the position of the button is always on. How to add a cookie method, if the button should be turned off so that it is remembered and the music does not sound? Help me please.

var radio = new Audio();
radio.src = "https://site.dvasyl.com/wp-content/uploads/2022/05/preview.mp3";
radio.loop = true;
radio.play();

document.querySelector('#muted').onclick = function() {
  if (radio.muted === true) {
   document.querySelector('#muted').innerHTML = '<img class="size_sound_alert" src="https://site.dvasyl.com/wp-content/uploads/2022/05/sound_on-5.svg" alt="Off" />'
    radio.muted = false;
  } else {
    document.querySelector('#muted').innerHTML = '<img class="size_sound_alert" src="https://site.dvasyl.com/wp-content/uploads/2022/05/sound_off-1.svg" alt="On" />'
    radio.muted = true;
  }  
}
    
    .size_sound_alert{
        width: 30px;
    }
    
.sound_label{
    position: fixed;
    margin-bottom: %;
    margin-left: 95%;
    top: 90%;
    z-index: 9999;
}
    
@media screen and (max-width: 768px) {
    .sound_label {
        position: fixed;
        margin-bottom: %;
        margin-left: 85%;
        top: 90%;
    }
}
<div id="muted" class="sound_label"><img class="size_sound_alert" src="https://site.dvasyl.com/wp-content/uploads/2022/05/sound_on-5.svg" alt="Off" /></div>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can set the cookie to whatever you need in your onClick event.

document.cookie adds a cookie to the browser.

<script>

var radio = new Audio();
radio.src = "https://site.dvasyl.com/wp-content/uploads/2022/05/preview.mp3";
radio.loop = true;
let isMuted = document.cookie;
radio.muted = isMuted == "muteCookie" ? true : false;
radio.play();

document.querySelector('#muted').onclick = function() {
  var soundAlert = document.querySelector('#muted');
  if (radio.muted) {
    soundAlert.innerHTML = '<img class="size_sound_alert" src="https://site.dvasyl.com/wp-content/uploads/2022/05/sound_on-5.svg" alt="Off" />'
    radio.muted = false;
    document.cookie = "";
  } else {
    soundAlert.innerHTML = '<img class="size_sound_alert" src="https://site.dvasyl.com/wp-content/uploads/2022/05/sound_off-1.svg" alt="On" />'
    radio.muted = true;
    document.cookie = "muteCookie";
  }  
}

</script>
about 4 years ago · Juan Pablo Isaza Relatório

0

// add elem to variable for single select from the dom;
const mutedElem = document.getElementById('muted');

var radio = new Audio();
radio.src = "https://site.dvasyl.com/wp-content/uploads/2022/05/preview.mp3";
radio.loop = true;

// you can't play audio without user action
// https://developer.chrome.com/blog/autoplay/
// radio.play();

// get value from localStorage and mute player if we need
if (!!localStorage.getItem('muted')) playerMute();

mutedElem.addEventListener('click', function () {
    // toggle muted param
    if (radio.muted) playerUnmute();
    else playerMute();
})

// mute player function
function playerMute() {
    mutedElem.innerHTML = '<img class="size_sound_alert" src="https://site.dvasyl.com/wp-content/uploads/2022/05/sound_off-1.svg" alt="On" />'
    radio.muted = true;
    localStorage.setItem('muted', 'true');
}

// unmute player function
function playerUnmute() {
    mutedElem.innerHTML = '<img class="size_sound_alert" src="https://site.dvasyl.com/wp-content/uploads/2022/05/sound_on-5.svg" alt="Off" />'
    radio.muted = false;
    localStorage.removeItem('muted');
}
body {
  background: gray;
}
<div id="muted" class="sound_label">
  <img class="size_sound_alert" src="https://site.dvasyl.com/wp-content/uploads/2022/05/sound_on-5.svg" alt="Off"/>
</div>

You had better use localStorage like in my answer.

p.s. you can't play audio without user action (please read this doc)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda