Playing sound in rails

2019-08-15 14:33发布

问题:

I'm currently using coffeescript to play wav file
I'm using this code below to play sound file

**my Coffeescript

audio = new Audio('error.wav');
audio.play();

I put error.wav in /app/assets/audio/error.wav

rails generate an error

Started GET "/touchtypings/error.wav" for 127.0.0.1 at 2014-01-28 11:08:04 +0700 Processing by TouchtypingsController#show

my understanding when it run the script rails is trying to interprett error.wav as route and process in controller

I just want to play the wav file when user has typed error.
thank you for help

回答1:

You'll need to give an absolute path to the audio file rather than a relative one. The relative path is resulting in GET /touchtypings/error.wav, which is why it's being picked up by the router.

Like this:

audio = new Audio('/error.wav'); // note the leading forward slash

And depending on your app config, the path you want is probably going to be /assets/error.wav rather than /error.wav