Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

288
Views
Audio: cómo verificar si la ruta es válida

Estoy tratando de reproducir un archivo de audio:

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

Si la ruta NO es válida, aparece la siguiente advertencia en la consola:

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

¿Existe un método de javascript para verificar si el archivo existe para que pueda evitar llamar al juego?

Me imagino que la respuesta podría estar en este documento:

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

Encontré una solución pero NO me gusta:

 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 answers
Answer question

0

Usar una promesa ayudó:

 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 Report

0

Tíralo en una promesa.

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!