D
D
Dmitry Marchenko2020-02-09 08:42:16
JavaScript
Dmitry Marchenko, 2020-02-09 08:42:16

JavaScript. The sound is interrupted when repeated. How to make sound repetition correctly?

I need to implement sound repetition in such a way that the sound repetition is not noticeable, in my code the sound first fades for a split second, and then repeats.

var sound = new Audio();
sound.src = "sounds/car.mp4";
sound.loop = true;
sound.play();

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Shohruh Shaimardonov, 2020-02-09
@joeberetta

Here is such a crutch found in the open spaces of CO

F
floydback, 2020-02-09
@floydback

The first option is to use callback timeupdate to keep track of how close the end is to rewind to the beginning. The disadvantage of this method is that this callback rarely fires and each repetition will be of unequal length. It can be heard. In addition, you can skip the end, because the callback will never work on the endpoint.
The second option is to do the same with the request animation frame. It works more often, but also has its drawbacks. If the lord is closed on weak devices Theoretically, it can be called less often. But from practice I can say that it works better and it is almost impossible to hear unevenness in duration.
In both cases, there may be such a situation: in a one-second file, a callback arrives with a parameter of 0.9799, and the next one is already 0. That is. bypassing 1. The reason is that the callback is generated periodically.
Third option. If you're doing something where a really even rhythm is important (like a metronome), the only way out is to read the audioBuffer, copy it many times, glue it into one long array, and play that long file. It works quickly and is very accurate.

X
xmoonlight, 2020-02-09
@xmoonlight

In the file itself, add 3 seconds of repetition on the left and right:
1. end of the main - the last 3 seconds,
2. main piece
3. start of the main - the first 3 seconds.
Then, when ready, play from the beginning of the main to the end of the main: just the middle!
When finished, set the position back to the beginning of the main one.
That is, you create your own software loop.

L
laisto, 2021-08-24
@laisto

Is it legal to shove mp4 into audio at all?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question