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

289
Vistas
Audio - how to check if path is valid

I am trying to play an audio file:

path = '/public/recordings/weekday/1.mp3'
const audio = new Audio(path)
audio.play()

If the path is NOT valid then the following warning appears in the console:

Uncaught (in promise) DOMException: Failed to load because no supported source was found.

is there a javascript method to check if the file exists so I can avoid calling play on it?

I imagine the answer could be in this doc:

https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/canPlayType

I did find a workaround but I do NOT like it:

audio.addEventListener('error', function (err) {
   if (err.path[0].error.message.includes('DEMUXER_ERROR_COULD_NOT_OPEN')) {
      this.handleError()
    } else {
      this.handleSomeOtherError()
    }
}.bind(this), false)
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Using a promise helped:

const playPromise = this.audio.play();
if (playPromise !== undefined) {
    playPromise.then(function() {
      console.log('Automatic playback started!!')
    }).catch(function(error) {
       console.log(error)
       this.handleError()
    }.bind(this), false);
 }
about 4 years ago · Juan Pablo Isaza Denunciar

0

Throw it in a promise.

function Function() {
path = 'https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3';
var audio = new Audio(path);
var playPromise = audio.play();
if (playPromise !== undefined) {
  playPromise.then(_ => {
    audio.setAttribute("controls", "controls");
    document.body.appendChild(audio);
    audio.play();
  })
  }
  }
<button onclick="Function()">Try it</button>

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