Why not my rewind audio?

I'm having trouble rewinding audio in Javascript. I basically have a countdown that beeps every second as it approaches the end of the countdown.

I tried to use;

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

but he didn’t beep every second, which I suppose has something to do with the need to download a new sound every second. Then I tried to download sound from outside and run it using the following code.

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

but it only plays the sound once, and then does not rewind it completely (this is shown by the console, recording the time 0.19 seconds after the first run).

Am I missing something here?

+5
source share
4 answers

In google chrome, I noticed that it only works if you have an audio file in the same domain. I have no problem if the audio file is in the same domain. Setting events .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 searched the search engine for a long time and could not find anything in the specification about this or even about any discussion.

+7
source

when I want to rewind, I just download the audio again:

 my_audio.load() 

* btw, I also use eventlistener for 'canplay' to run my_audio.play (). This seems to be necessary in android, and maybe other devices also *

+3
source

To continue dsdsdsdsd, answer a bit with the numbers for "the whole shebang"

NOTE. In my application, loc1 is a dummy that refers to the stored location of the song

 // ... code before ... tune = new Audio(loc1); // First, create audio event using js // set up "song over' event listener & action tune.addEventListener('ended', function(){ tune.load(); //reload audio event (and reset currentTime!) tune.play(); //play audio event for subsequent 'ended' events },false); tune.play(); // plays it the first time thru // ... code after ... 

I spent days and days trying to figure out what I'm doing wrong, but now everything is working fine ... at least on desktop browsers ...

0
source

Starting with Chrome version 37.0.2062.120 m behavior described by @ Esailija has not changed.

I will get around this problem by encoding base64 encoded audio data and submitting the data to Audio() using data: URL.

Test code:

 snd = new Audio('data:audio/ogg;base64,[...base64 encoded data...]'); snd.onloadeddata = function() { snd.currentTime = 0; snd.play(); setTimeout(function() { snd.currentTime = 0; snd.play(); }, 200); }; 

(I'm surprised there are no error messages or links to this question ... or maybe my Google-fu is not strong enough.)

0
source

Source: https://habr.com/ru/post/980673/


All Articles