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.
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.
Share
Improve this question
edited Oct 21, 2022 at 14:37
Luca Kiebel
10.1k7 gold badges32 silver badges46 bronze badges
asked Jul 20, 2017 at 13:39
Mikael Sauriol SaadMikael Sauriol Saad
1221 gold badge2 silver badges10 bronze badges
5
|
5 Answers
Reset to default 9Put 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
Try giving the absolute path instead of relative one.
use:
./Content/Sound/Down.mp3
or this:
/Content/Sound/Down.mp3
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>
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
const audio = new Audio('/file/ringtone.mp3');
if you use ~
you will get an error, for example:
const audio = new Audio('~/file/ringtone.mp3');
wwwdata
or something similar)? – Halcyon Commented Jul 20, 2017 at 13:41http://localhost:5/~/Content/Sound/Down.mp3
from your browser?~
means that you start at your home directory – Luca Kiebel Commented Jul 20, 2017 at 13:42