diff --git a/src/components/beat-loader.js b/src/components/beat-loader.js index 7de777b..ad0a59b 100644 --- a/src/components/beat-loader.js +++ b/src/components/beat-loader.js @@ -139,7 +139,8 @@ AFRAME.registerComponent('beat-loader', { // Re-sync song with beats playback. const songComponent = this.el.components.song; const currentTime = songComponent.getCurrentTime(); - if (songComponent.songStartTime && this.beatsTimeOffset !== undefined && + + if (currentTime !== undefined && this.beatsTimeOffset !== undefined && this.songCurrentTime !== currentTime) { this.songCurrentTime = currentTime; this.beatsTime = (this.songCurrentTime + this.data.beatAnticipationTime) * 1000; @@ -175,6 +176,7 @@ AFRAME.registerComponent('beat-loader', { } } + // Update preload beat offset. if (this.beatsTimeOffset !== undefined) { if (this.beatsTimeOffset <= 0) { this.el.sceneEl.emit('beatloaderpreloadfinish', null, false); @@ -185,7 +187,7 @@ AFRAME.registerComponent('beat-loader', { } } - this.beatsTime = beatsTime + delta; + this.beatsTime += delta; }, generateBeat: (function () { @@ -302,7 +304,7 @@ AFRAME.registerComponent('beat-loader', { */ clearBeats: function () { this.beatsTime = 0; - this.beatsTimeOffset = undefined; + this.beatsTimeOffset = this.data.beatAnticipationTime * 1000; for (let i = 0; i < this.beatContainer.children.length; i++) { let child = this.beatContainer.children[i]; if (child.components.beat) { diff --git a/src/components/song.js b/src/components/song.js index 56347d2..4aa4fd6 100644 --- a/src/components/song.js +++ b/src/components/song.js @@ -132,6 +132,7 @@ AFRAME.registerComponent('song', { this.source.stop(); this.source.disconnect(); this.source = null; + this.songStartTime = undefined; }, victory: function () { @@ -178,6 +179,7 @@ AFRAME.registerComponent('song', { if (this.data.isBeatsPreloaded) { this.startAudio(); } }, ONCE); this.audioAnalyser.refreshSource(); + this.songStartTime = undefined; }, onWallHitStart: function () { @@ -200,6 +202,7 @@ AFRAME.registerComponent('song', { }, getCurrentTime: function () { + if (this.songStartTime === undefined) { return undefined; } return this.context.currentTime - this.songStartTime; } });