Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

javascript - 如何获取AudioBufferSourceNode的当前时间?(How can I get an AudioBufferSourceNode's current time?)

When working with audio elements ( <audio> ) or contexts( AudioContext ), you can check their currentTime property to know exactly the play time of your buffer.(当使用音频元素( <audio> )或上下文( AudioContext )时,可以检查它们的currentTime属性以确切了解缓冲区的播放时间。)

All of this is fine an dandy until I created multiple sources (or AudioBufferSourceNode ) in a single AudioContext .(在我在一个AudioContext创建多个源(或AudioBufferSourceNode )之前,所有这些都是不错的选择。) The sources could be played at different times, therefore I would need to know their corresponding currentTime 's, to illustrate:(源可以在不同的时间播放,因此我需要知道它们对应的currentTime ,以说明:) 与播放时间未知的音源关联的音频上下文。 Some base code for you to work off:(一些基础代码供您解决:) buffer1 = [0,1,0]; //not real buffers buffer2 = [1,0,1]; ctx = new AudioContext(); source1 = ctx.createBufferSourceNode(); source1.buffer = buffer1; source1.connect(ctx.destination); source1.start(0); source2 = ctx.createBufferSourceNode(); source2.buffer = buffer2; source2.connect(ctx.destination); setTimeout(1000/*some time later*/){ source2.start(0); } setTimeout(1500/*some more time later*/){ getCurrentTime(); } function getCurrentTime(){ /* magic */ /* more magic */ console.log("the sources currentTime values are obviously 1500 (source1) and 500 (source2)."); }   ask by undefined translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What I usually do is create a wrapper for the audio source node that keeps track of the playback state.(我通常要做的是为音频源节点创建一个包装,以跟踪播放状态。)

I've tried to minimise the code below to show the basics.(我试图最小化下面的代码以显示基础知识。) The core idea is to keep track of the time the sound is started and the time the sound is 'paused' and use those values to get the current time and to resume playback from the paused position.(核心思想是跟踪声音的开始时间和声音“暂停”的时间,并使用这些值来获取当前时间并从暂停的位置恢复播放。) I put a working example on codepen(我在Codepen上放了一个工作示例) function createSound(buffer, context) { var sourceNode = null, startedAt = 0, pausedAt = 0, playing = false; var play = function() { var offset = pausedAt; sourceNode = context.createBufferSource(); sourceNode.connect(context.destination); sourceNode.buffer = buffer; sourceNode.start(0, offset); startedAt = context.currentTime - offset; pausedAt = 0; playing = true; }; var pause = function() { var elapsed = context.currentTime - startedAt; stop(); pausedAt = elapsed; }; var stop = function() { if (sourceNode) { sourceNode.disconnect(); sourceNode.stop(0); sourceNode = null; } pausedAt = 0; startedAt = 0; playing = false; }; var getPlaying = function() { return playing; }; var getCurrentTime = function() { if(pausedAt) { return pausedAt; } if(startedAt) { return context.currentTime - startedAt; } return 0; }; var getDuration = function() { return buffer.duration; }; return { getCurrentTime: getCurrentTime, getDuration: getDuration, getPlaying: getPlaying, play: play, pause: pause, stop: stop }; }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...