Web Audio APIの使用をおすすめします。そこから、バッファ・ソース・ノードを100msまたは50msごとに、または任意の時間にループすることができます。他の回答で述べたように がこれを行うには、あなたは今すぐ
// set up the Web Audio context
var audioCtx = new AudioContext();
// create a new buffer
// 2 channels, 4410 samples (100 ms at 44100 samples/sec), 44100 samples per sec
var buffer = audioCtx.createBuffer(2, 4410, 44100);
// load the sound file via an XMLHttpRequest from a server
var request = new XMLHttpRequest();
request.open('GET', '/sound/BEAT.wav', true);
request.responseType = 'arraybuffer';
request.onload = function() {
var audioData = request.response;
audioCtx.decodeAudioData(audioData, function (newBuffer) {
buffer = newBuffer;
});
}
request.send();
あなたが再生
// create the buffer source
var bufferSource = audioCtx.createBufferSource();
// set the buffer we want to use
bufferSource.buffer = buffer;
// set the buffer source node to loop
bufferSource.loop = true;
// specify the loop points in seconds (0.1s = 100ms)
// this is a little redundant since we already set our buffer to be 100ms
// so by default it would loop when the buffer comes to an end (at 100ms)
bufferSource.loopStart = 0;
bufferSource.loopEnd = 0.1;
// connect the buffer source to the Web Audio sound output
bufferSource.connect(audioCtx.destination);
// play!
bufferSource.start();
ループにバッファソースノードを作ることができ、サーバを経由して、サウンドファイルをロードするためにXMLHttpRequestを使用する必要があります
bufferSource.stop()
で再生を停止すると、もう一度再生を開始できません。 start()
に一度しか電話することはできませんので、もう一度再生を開始するには、新しいソースノードを作成する必要があります。
XMLHttpRequest
を介してサウンドファイルがロードされるため、サーバーを実行しないでマシンでこれをテストしようとすると、ほとんどのブラウザで相互参照要求エラーが発生することに注意してください。だから、マシン上でこれをテストしたい場合、これを回避する最も簡単な方法は、Python SimpleHTTPServerを実行することです。
_ "遅い"とはどういう意味ですか? – guest271314
あなたのBEAT.wavはどれくらい持続するのですか?あなたはそれへのリンクを共有できますか? – Arnauld
100ms後に毎回サウンドを再生する必要があります(タイムアウトarg 2を100に設定すると)が、ビート間に約30ms〜100msあります。 – Chalic