4

So I've put my sound file in a sub folder of my code directory and whenever I try to play it, it says it can't find it.

Here's my code:

PlaySound = function () {
    var audio = new Audio('~/Content/Sound/Down.mp3');
    audio.loop = false;
    audio.play(); 
}

Anyone know why?

This is the error I get when I inspect:

GET http://localhost:5/~/Content/Sound/Down.mp3 
localhost/:1 Uncaught (in promise) DOMException: Failed to load 
because no supported source was found.
5
  • Can you open the file directly in the browser? Jul 20, 2017 at 13:41
  • Is the Content folder in the web document root (ie wwwdata or something similar)?
    – Halcyon
    Jul 20, 2017 at 13:41
  • can you access http://localhost:5/~/Content/Sound/Down.mp3 from your browser? ~ means that you start at your home directory Jul 20, 2017 at 13:42
  • give your file path correctly
    – Durga
    Jul 20, 2017 at 13:46
  • Not sure @Halcyon, but it's in my code. I've put multiple pictures another folder in code which works fine, but this one, it can't read it, I've taken the pictures from the same file path but from pictures folder. Jul 20, 2017 at 13:47

4 Answers 4

9

Put the soundfile in the same directory that your HTML file is in.

after that, this code should not give you any errors:

PlaySound = function () {
    var audio = new Audio('Down.mp3');
    audio.loop = false;
    audio.play(); 
}

Your Broswer has to be able to access the Audio file, so if you visit http://localhost:5/~/Content/Sound/Down.mp3, it should actually open the file

1

Try giving the absolute path instead of relative one.

use: ./Content/Sound/Down.mp3

or this: /Content/Sound/Down.mp3

0

why don't you use the html5 audio tag

<audio controls>
  <source src="sample.ogg" type="audio/ogg">
  <source src="sample.mp3" type="audio/mpeg">
</audio>
3
  • 1
    This does not answer the question. You could perhaps post it as a comment.
    – Halcyon
    Jul 20, 2017 at 13:43
  • i don't have that much privileges to comment on question
    – varnit
    Jul 20, 2017 at 13:50
  • why don't you try absolute path instead of relative path
    – varnit
    Jul 20, 2017 at 13:51
0

I think the answer is you can't play an audio before the user interact with the window, you need to do PlaySound function clickable in button or something like that

New contributor
Nicolas Zarate is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Nov 24 at 12:08

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.