kittykatattack / sound.js

A micro-library to load, play and generate sound effects and music for games and interactive applications
300 stars 44 forks source link

Is there a way to get the current playback time? #14

Closed CloverFeywilde closed 7 years ago

CloverFeywilde commented 7 years ago

I'm making a psudo rhythm game using Pixi.JS right now, and needed a lightweight sound solution! This looks perfect, but is there any way, while a song is playing, for me to get the current time returned to me? That way I can sync game events to specific points in the song.

ghost commented 7 years ago

You can use var time = sounds['src'].soundNode.context.currentTime to get elapsed time after sounds['src'] was created. To calculate current playing time you can store the currentTime once sound is played, and time - stored is what you want.

ghost commented 7 years ago

I just realized that sound.js has already record o.startTime = actx.currentTime; when calling play function, so you can simply use sounds['src'].soundNode.context.currentTime - sounds['src'].startTime instead :)

CloverFeywilde commented 7 years ago

Thanks so much! This worked like a charm.