1

场景:使用Javascript - 寻求在准确的位置时声音一定位置音轨

  1. 音频开始从0:00播放。在0:05处,轨迹跳转到0:30
  2. 轨道立即开始在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

真正的问题出现在需要立即无缝跳过时。例如,setTimeoutvery 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或方法,你有想法!

+0

工作小提琴?也许?只想玩 –

回答

2

当您实际调用AudioBufferSourceNode上的start()时,您不会显示,因此我无法显式调试。但总而言之,您需要计划停止并在需要发生时启动AHEAD,而不是尝试在setTimeout回调中“交换”播放的活动声音(或者在声音停止播放时尝试让它们对齐)。

例如,你会做这样的事情:

var bs1 = audioCtx.createBufferSourceNode(); 
var bs2 = audioCtx.createBufferSourceNode(); 
var bs3 = audioCtx.createBufferSourceNode(); 
var now = audioCtx.currentTime + 0.020; // add 20ms for scheduling slop 

bs1.buffer = audioBuffer; 
bs1.connect(audioCtx.destination); 
bs2.buffer = audioBuffer; 
bs2.connect(audioCtx.destination); 
bs3.buffer = audioBuffer; 
bs3.connect(audioCtx.destination); 
bs1.start(now, 0, 5); // time, offset, duration 
bs2.start(now+5, 30, 5); 
bs3.start(now+10, 5); // no duration; play to end 

如果要取消这个游戏,你必须断开buffersource节点。

+0

感谢您的答案!是的,像这样的东西会工作我想,但我需要先测试它。什么是20ms?在无数次排队跳过的情况下,进行此操作的最佳方式是什么? –

+0

20ms(或更多)是因为您需要在WebAudio时间轴中安排事情,才能实际发生。 cf两个时钟的故事:https://www.html5rocks.com/en/tutorials/audio/scheduling/。 – cwilso

1

其中的主要问题是时间的准确性,从0:05到0:30以及从0:35到0:05的无缝转换。

我想你应该创建两个不同的音频源。从0:00开始,在0:30寻找另一个,但在开始使用事件播放时立即暂停。

然后使用ontimeupdate事件跟踪时间并检查它是否为5,然后暂停当前并开始第二个应该被缓冲的时间,当它到达0:35时暂停并启动第一个并让它播放直到音频结束。

+0

感谢您的建议!我已经试过这个,不认为它会满足精确度的要求,并立即通过赛道寻找。 https://stackoverflow.com/a/12325960/2957677 –

1

播放:0:00到0.05,跳过:0:05到0:30,播放:0:30到0:35,跳过:0:35 到0:05,播放:0.05到结束

您可以使用Media Fragments URIpause事件来按顺序呈现精确的媒体播放。如果必要的话通过HTMLMediaElementAudioContext.createMediaElementSource()

const audio = document.querySelector("audio"); 
 

 
const mediaURL = "https://ia600305.us.archive.org/30/items/return_201605/return.mp3"; 
 

 
const timeSlices = ["#t=0,5", "#t=30,35", "#t=5"]; 
 
let index = 0; 
 
audio.onpause = e => { 
 
    if (index < timeSlices.length) { 
 
    audio.src = `${mediaURL}${timeSlices[index++]}` 
 
    } else { 
 
    console.log("Media fragments playlist completed"); 
 
    } 
 
} 
 
audio.ontimeupdate = e => console.log(Math.ceil(audio.currentTime)); 
 
audio.oncanplay = e => audio.play(); 
 
audio.src = `${mediaURL}${timeSlices[index++]}`;
<audio controls></audio>

+0

感谢您的回答!我不知道你可以这样做。但是,在这个例子中,转换不是一个“立即无缝跳过”。我将在整首歌中毫无节奏地过渡,所以重要的一点是转换发生在一个非常精确的点上,但是使用'ontimeupdate'不能提供足够的精度(https://stackoverflow.com/a/12325960/2957677) –

+0

@TobyMellor'timeupdate'事件仅用于显示媒体播放的精确'.currentTime'。 'pause'事件处理程序是下一个媒体分区被设置为'.src''

+0

@TobyMellor另请参见[HTML5音频流:精确测量延迟?](https://stackoverflow.com/questions/38768375/html5-audio-streaming-precisely-measure-latency) – guest271314