場景:使用Javascript - 尋求在準確的位置時聲音一定位置音軌
- 音頻開始從0:00播放。在0:05處,軌跡跳轉到0:30。
- 軌道立即開始在0:30玩耍,正好0:35,軌道向後跳轉到0:05和播放音頻文件
摘要的其餘部分: 播放:0:00至0.05,跳過:0:05至0:30,播放:0:30至0:35,跳過:0:35至0:05,播放:0.05到END
真正的問題出現在需要立即無縫跳過時。例如,setTimeout
是very inaccurate並且意味着在這種情況下不能使用漂移。
我試圖使用Web Audio API來實現這一點,但我仍然無法立即轉換。我目前正在使用AudioBufferSourceNode.start(when, offset, duration);
安排加載歌曲的片段,最初以上述示例的(0 [when], 0 [offset], 0:05 [duration])
傳遞。在此之後,我仍然使用setTimeout
調用相同的功能,運行時間短至0:30並安排了類似(0 [when] + 0:05 [previous duration], 0:30 [offset], 0:05 [duration])
的計劃。這
var AudioContext = window.AudioContext || window.webkitAudioContext,
audioCtx = new AudioContext();
var skipQueue = [0, 5, 30, 35, 5];
// getSong(); // load in the preview song (https://audiojungle.net/item/inspiring/9325839?s_rank=1)
playSound(0, 0);
function getSong() {
console.log("Loading song...");
request = new XMLHttpRequest();
// request.open('GET', "https://preview.s3.envato.com/files/230851789/preview.mp3?response-content-disposition=attachment%3Bfilename%3D20382637_uplifting-cinematic-epic_by_jwaldenmusic_preview.mp3&Expires=1501966849&Signature=HUMfPw3b4ap13cyrc0ZrNNumb0s4AXr7eKHezyIR-rU845u65oQpxjmZDl8AUZU7cR1KuQGV4TLkQ~egPt5hCiw7SUBRApXw3nnrRdtf~M6PXbNqVYhrhfNq4Y~MgvZdd1NEetv2rCjhirLw4OIhkiC2xH2jvbN6mggnhNnw8ZemBzDH3stCVDTEPGuRgUJrwLwsgBHmy5D2Ef0It~oN8LGG~O~LFB5MGHHmRSjejhjnrfSngWNF9SPI3qn7hOE6WDvcEbNe2vBm5TvEx2OTSlYQc1472rrkGDcxzOHGu9jLEizL-sSiV61uVAp5wqKxd2xNBcsUn3EXXnjMIAmUIQ__&Key-Pair-Id=APKAIEEC7ZU2JC6FKENA", true); // free preview from envato
request.responseType = "arraybuffer";
request.onload = function() {
var audioData = request.response;
audioCtx.decodeAudioData(audioData, function(buffer) {
audioBuffer = buffer;
console.log("Ready to play!");
playSong()
}, function(e) {
"Error with decoding audio data" + e.err
});
}
request.send();
}
function playSound(previousPlay, nextPlay) {
// source = audioCtx.createBufferSource();
// source.buffer = audioBuffer;
// source.connect(audioCtx.destination);
skipQueue.shift();
var duration = Math.abs(skipQueue[0] - previousPlay);
// source.start(nextPlay, previousPlay, duration);
console.log("Running: source.start(" + nextPlay + ", " + previousPlay + ", " + duration + ")");
console.log("Current Time: " + previousPlay);
console.log("Next Play in: " + duration + " (Skipping from " + skipQueue[0] + " to " + skipQueue[1] + ")");
if (skipQueue.length > 1) {
setTimeout(function() {
playSound(skipQueue[0], nextPlay + duration);
}, 1000 * duration - 50); // take 50ms off for drift that'll be corrected in scheduling
}
}
<strong>Expected:</strong><br />
Play: 0:00 to 0.05, Skip: 0:05 to 0:30, Play: 0:30 to 0:35, Skip: 0:35 to 0:05, Play: 0.05 to END
的例子我能不能得到這個簡化的例子下降100%的工作,但你可以看到我的嘗試是在控制檯中的內容。由於StackOverflow不支持AJAX,我還評論了代碼。
我打算使用任何其他的API或方法,你有想法!
工作小提琴?也許?只想玩 –