最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

mp3 - Play sound in javascript - Stack Overflow

programmeradmin0浏览0评论

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
  • Can you open the file directly in the browser? – evolutionxbox Commented Jul 20, 2017 at 13:41
  • Is the Content folder in the web document root (ie wwwdata or something similar)? – Halcyon Commented 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 – Luca Kiebel Commented Jul 20, 2017 at 13:42
  • give your file path correctly – Durga Commented 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. – Mikael Sauriol Saad Commented Jul 20, 2017 at 13:47
Add a comment  | 

5 Answers 5

Reset to default 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

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');

发布评论

评论列表(0)

  1. 暂无评论