1

So I have this mp3 file called "beep" on my desktop, and I want to use javascript to play it. I know it's possible to use direct links to audio files on websites to play audios with javascript, but can I play it from my desktop too? If so, how do I do it, and what am I doing wrong?

var audio = new Audio("C:\Users\nmelssx\Desktop\beep");
audio.play();
frosty
  • 2,559
  • 8
  • 37
  • 73
  • I'm no expert when it comes to HTML, but one thing that is wrong is when this page will be rendered on other user's browser, `C:\Users\nmelssx\Desktop\beep` would simply not exists. – Anand Undavia Aug 23 '18 at 07:55
  • Assuming this is just something you're doing locally, for own use only, make sure that you have the "rights" to access the directory. If you attempt your current approach, what happens? – Martin Aug 23 '18 at 07:57
  • @Martin Yeah I'm just doing this locally for my own personal use only. When I tried this code, the sound doesn't play. – frosty Aug 23 '18 at 07:59
  • What does the console log say? (ctrl+shift+i in Chrome > Console tab). Do you have any error messages? Also, what filetype is the file you're trying to play? – Martin Aug 23 '18 at 08:05
  • @Martin console log says that google chrome block local files. – frosty Aug 23 '18 at 08:19
  • Well there you go. It's probaby to prevent potential abuse. Seems like you need to set up some sort of web server that allows you to access local files, or simply create a wamp server directory or similar. So... accessing files from the desktop seems like a rough thing to do :/ personally I use WAMP for localhost solutions. – Martin Aug 23 '18 at 08:22
  • @Martin What kind of abuse can google chrome possibly prevent by not allowing us to use local files? – frosty Aug 23 '18 at 08:31
  • Some local files are static, like what you have in %appdata% etc. that you normally don't want other people to have access to. Imagine if I could access any default directory by wildcarding. I personally wouldn't want that :P – Martin Aug 23 '18 at 08:37

1 Answers1

0

When you create new Audio object, the element has an attribute of src with the address you provided.
In order to load soures from desktop via src you need to use the prefix "file://" before your address.
However, chrome blocks local rsources. There are solutions for that here.

Maor Refaeli
  • 2,417
  • 2
  • 19
  • 33