skip to Main Content

I would like to play audio file from D drive in javascript, I’m getting this error:

Uncaught (in promise) DOMException: The media resource indicated by the src attribute or assigned media provider object was not suitable.

My code:

var audio, audio_outcome, audio_path, audio_file_0, audio_file_1;

func_playAudio();
function func_playAudio(){

audio_path = 'D:\VM\vmShare\Testing\JS\admin\audio';
audio_file_0 = 'boom.mp3';
audio_file_1 = 'thunder.mp3';
audio_outcome = 0;

if (audio_outcome == 0){
    audio = new Audio(audio_path+'\'+audio_file_0);
    audio.play();
}else if (audio_outcome == 1){
    audio = new Audio(audio_path+'\'+audio_file_1);
    audio.play();
}
}

2

Answers


  1. Chosen as BEST ANSWER

    I came to learn that javascript cannot access files from another drive (or locally as I'm getting same error with files in the same directory where the script is running from?)

    I ended up uploading the files to an FTP server and accessing the audio files using a URL instead.

    This works (the only change from my function is the "audio_path"):

    audio_path = 'https://<yourDomain.com>/backup/vmShare/Testing/JS/admin/audio';
    

  2. I’m not sure how you plan to run this Javascript code but I’d say you will either have to use, for example, ExpressJS web server and download the files from, say, https://localhost:3000/audio/file1.mp3, OR you could try to store the file directly in the script in Base64, if they are small.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search