mirror of
https://github.com/jiojciojsioe3/a3cjroijsiojiorj.git
synced 2024-11-15 07:21:50 +08:00
Merge pull request #1 from LoveEevee/master
Fix pausing in bufferedloop
This commit is contained in:
commit
1a0cf0bf3f
@ -5,8 +5,8 @@ class BufferedLoop{
|
|||||||
this.context=new AudioContext()
|
this.context=new AudioContext()
|
||||||
this.buffers=[]
|
this.buffers=[]
|
||||||
this.sources=new Set()
|
this.sources=new Set()
|
||||||
|
this.loadCallback=[]
|
||||||
this.bufferedTime=0
|
this.bufferedTime=0
|
||||||
this.iteration=0
|
|
||||||
this.bgm1=bgm1
|
this.bgm1=bgm1
|
||||||
this.bgm2=bgm2
|
this.bgm2=bgm2
|
||||||
this.loadSound(bgm1.url,0)
|
this.loadSound(bgm1.url,0)
|
||||||
@ -28,12 +28,14 @@ class BufferedLoop{
|
|||||||
setLoaded(){
|
setLoaded(){
|
||||||
if(this.buffers[0]&&this.buffers[1]){
|
if(this.buffers[0]&&this.buffers[1]){
|
||||||
this.loaded=true
|
this.loaded=true
|
||||||
if(this.loadCallback){
|
for(var i in this.loadCallback){
|
||||||
this.loadCallback()
|
this.loadCallback[i]()
|
||||||
delete this.loadCallback
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
onLoad(callback){
|
||||||
|
this.loadCallback.push(callback)
|
||||||
|
}
|
||||||
playSound(buffer,time,duration){
|
playSound(buffer,time,duration){
|
||||||
var self=this
|
var self=this
|
||||||
var source=this.context.createBufferSource()
|
var source=this.context.createBufferSource()
|
||||||
@ -41,11 +43,14 @@ class BufferedLoop{
|
|||||||
source.connect(this.context.destination)
|
source.connect(this.context.destination)
|
||||||
source.start(time)
|
source.start(time)
|
||||||
this.bufferedTime=time+duration
|
this.bufferedTime=time+duration
|
||||||
this.sources.add(source)
|
var sourceObject={
|
||||||
setTimeout(function(){
|
source:source,
|
||||||
self.sources.delete(source)
|
timeout:setTimeout(function(){
|
||||||
|
self.sources.delete(sourceObject)
|
||||||
},duration*1000)
|
},duration*1000)
|
||||||
}
|
}
|
||||||
|
this.sources.add(sourceObject)
|
||||||
|
}
|
||||||
addLoop(){
|
addLoop(){
|
||||||
if(this.context.currentTime>this.bufferedTime-1){
|
if(this.context.currentTime>this.bufferedTime-1){
|
||||||
this.playSound(
|
this.playSound(
|
||||||
@ -59,10 +64,12 @@ class BufferedLoop{
|
|||||||
play(){
|
play(){
|
||||||
var self=this
|
var self=this
|
||||||
if(!this.loaded){
|
if(!this.loaded){
|
||||||
this.loadCallback=this.play
|
return this.onLoad(function(){
|
||||||
return
|
self.play()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
this.start=this.context.currentTime+0.1
|
this.start=this.context.currentTime+0.1
|
||||||
|
this.iteration=0
|
||||||
this.playSound(
|
this.playSound(
|
||||||
this.buffers[0],
|
this.buffers[0],
|
||||||
this.start,
|
this.start,
|
||||||
@ -75,8 +82,9 @@ class BufferedLoop{
|
|||||||
}
|
}
|
||||||
pause(){
|
pause(){
|
||||||
clearInterval(this.interval)
|
clearInterval(this.interval)
|
||||||
this.sources.forEach(function(source){
|
this.sources.forEach(function(sourceObject){
|
||||||
source.stop(0)
|
sourceObject.source.stop(0)
|
||||||
|
clearTimeout(sourceObject.timeout)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user