8

I'm trying to use this javascript to loop an audio element:

music.js:

myAudio = new Audio('/assets/drumloop.mp3');
myAudio.loop = true;
myAudio.play(); 

When I include this as a script in a plain html file and open the html file in Safari 5.1, it loops just fine. When I include this javascript from my Rails application running on a local rails server, the audio plays, but does not loop.

I have tried using a callback on the 'ended' event to set the time to zero and play again (as suggested here, but that does not work either.

Is it possible that rails isn't sending enough information in the http header?

Community
  • 1
  • 1
  • Strange... It works once it's pushed to heroku, so maybe it has something to do with serving precompiled assets... Back to the http header thing... I'll debug this if I get around to it, but in the mean time it's good enough! –  Apr 20 '12 at 18:09
  • 1
    if you don't put it in the asset pipeline, does it still have the problem? – Matt Apr 20 '12 at 18:50

2 Answers2

0

You dont need JS for this. Try this HTML:

<audio controls="controls" loop="loop">
  <source src="/assets/drumloop.mp3" type="audio/mpeg" />
</audio>

The key is loop="loop"

oleurud
  • 58
  • 6
  • Thanks, but the problem is that it doesn't obey these tags when it's served by the local rails server. I have too much other stuff to do so I haven't had a chance to properly debug it, but as I said, I suspect there's some extra header information it's sending when the audio data is served by heroku. –  Apr 30 '12 at 16:19
0

I frequently run into the same issue. I haven't found a real solution, but did come of with a "good enough during development" one.

I ended up throwing my audio files on another server and referencing them from my rails app during development if I really needed to test looping or seeking.

Apache running on localhost worked fine for me. You could also use a web server. Just make sure to change those paths back before deploying!

Super frustrating.

Dan Tello
  • 1,507
  • 1
  • 10
  • 10