Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

170
Vistas
I want to pause current playing audio when another audio start

I have an web application when click the artist image that plays audio files. I'm pretty new to this. I found a few solutions on the internet, but I couldn't do it. I want to pause current playing audio when another audio start.but it continues to play in the background , but what I want is to pause and continuing playing from that point forward. So far, I could not make that change.

function switchCase(key){
    switch (key) {
        case 'mozart-img':
            var audio = new Audio("sounds/mozart.mp3")
            audio.play();
            break;
        case 'beethoven-img':
            var audio = new Audio("sounds/beethoven.mp3")
            audio.play();
            break;
        case 'chopin-img':
            var audio = new Audio("sounds/chopin.mp3")
            audio.play();
            break;
        case 'schumann-img':
            var audio = new Audio("sounds/schumann.mp3")
            audio.play();
            break;                
        default:
            break;
    }
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Using an IIFE here so no globals will be required (other than the function switchCase which you have already anyway)

const switchCase = (function() {
    let playing;
    function stop() {
        if (playing) {
            playing.removeEventListener('ended', stop); //*
            playing.pause();
            playing = null;
        }
    }
    return function (key){
        stop();
        switch (key) {
            case 'mozart-img':
                playing = new Audio("sounds/mozart.mp3");
                break;
            case 'beethoven-img':
                playing = new Audio("sounds/beethoven.mp3");
                break;
            case 'chopin-img':
                playing = new Audio("sounds/chopin.mp3");
                break;
            case 'schumann-img':
                playing = new Audio("sounds/schumann.mp3");
                break;                
            default:
                break;
        }
        playing.play();
        playing.addEventListener('ended', stop); //*
    };
})();

So, playing holds the currently playing audio object - before creating a new one, simply stop the currently playing one (if it exists)

Added "ended" event handler - probably overkill, but just remove the lines with addEventListener and removeEventListener (marked with //* ) to get rid of probably redundant event handling

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda