Play .mp3 if .wav file is not found


#1

I have some audio assets combined with mp3 and wav file. I have an application that loops through the data name and just combine it with the extension and get the source of the audio based on the name like the code below. My problem is that it only get the .wav file.

setTimeout( () => {

      this.audio.src = '../assets/audio/'+this.type+'/'+voice+'.wav';

      if (!this.audio.src) {
        this.audio.src = '../assets/audio/'+this.type+'/'+voice+'.mp3';
      }
      
      this.audio.play();
    }, 400);

What will I do to play the .mp3 if the .wav is not found?

Currently my code is not working.


#2

you have to load the file and test if its not there, not testing if your path value is empty - which it never is, cause you defined it above.

You currently do:

if (!('../assets/audio/'+this.type+'/'+voice+'.wav'))

#3

Hi, right now, I’m loading my audio directly by setting the source. But I’m not really sure how to load the file first and test. Can you help me some idea on what you mean? thank you.


#4

use any file plugin to test on wether the loading works to find out if the file exists. When your lucky you may find a plug-in with which you can test if it exists directly.


#5

You did something which can’t work ever, its basically the same as:

var aString = "hello world";
if(!aString){
// this will never run
}