Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't my audio rewinding?

I'm having a bit of trouble rewinding audio in Javascript. I basically have a countdown that beeps each second as it gets towards the end of the countdown.

I tried using;

var bip = new Audio("http://www.soundjay.com/button/beep-7.wav");
bip.play();

but it didn't beep every second which I'm guessing has something to do withit having to load a new sound every second. I then tried loading the sound externally and triggering it with the following code.

bip.pause();
bip.currentTime = 0;
console.log(bip.currentTime);
bip.play();

but this only plays the sound once then completely fails to rewind it (this is shown by the console logging a time of 0.19 seconds after the first playthrough).

Is there something I'm missing here?

like image 643
Sam Beckham Avatar asked Jun 12 '12 20:06

Sam Beckham


2 Answers

In google chrome I noticed that it only works if you have the audio file in same domain. I have no problems if the audio file is in same domain. Event setting .currentTime works.

Cross-domain:

var bip = new Audio("http://www.soundjay.com/button/beep-7.wav");
bip.play();
bip.currentTime; //0.10950099676847458
bip.currentTime = 0;
bip.currentTime; //0.10950099676847458

Same-domain:

var bip = new Audio("beep-7.wav");
bip.play();
bip.currentTime; //0.10950099676847458
bip.currentTime = 0;
bip.currentTime; //0

I tried googling for a while and could find nothing in specs about this or even any discussion.

like image 176
Esailija Avatar answered Sep 20 '22 11:09

Esailija


when I want rewind I simply load the audio again:

my_audio.load()

*btw, I also use a eventlistener for 'canplay' to trigger the my_audio.play(). It seems that this is necessary in android, and maybe other devices also*

like image 40
dsdsdsdsd Avatar answered Sep 18 '22 11:09

dsdsdsdsd