-
Notifications
You must be signed in to change notification settings - Fork 132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sound.currentTime Needed #140
Comments
I believe you can start the sound from a certain point, but I don't believe you can have something like an interactive song position that is changeable at any time, typical with modern video and audio players on the web. Of course anything is better than nothing. |
@Nascent ow can we do that? |
Yea we need this. But even better would be awesome to get the current play back time for each individual sound object within a group object |
might be possible with a little mathematics... |
I also ran into this issue using Pizzicato. Here's the basic moving pieces I used to solve the issue: const state = { // transport state
type: "stop", // sound is stopped
playhead: 0, // current playhead position
timeRef: null, // time reference when play was clicked
}
const sound = new Pizzicato.Sound(...) Now implement your transport controls: function play() {
switch (state.type) {
case "stop":
state.type = "play" // change play state
transport.timeRef = Date.now() // record time reference
sound.play(0, state.playhead) // play sound from current playhead
break
case "play":
break // sound is already playing
}
}
function stop() {
switch (state.type) {
case "stop":
break // sound is already stopped
case "play":
const delta = (Date.now() - state.timeRef) / 1000 // compute time delta
state.type = "stop" // change play state
state.playhead = state.playhead + delta // update playhead
state.timeRef = null // unset time reference
break
}
} Now you can get the playhead position at any time: requestAnimationFrame(function onFrame() {
switch (state.type) {
case "stop":
console.log("playhead", state.playhead) // render
break
case "play":
const delta = (Date.now() - state.timeRef) / 1000
console.log("playhead", state.playhead + delta) // render
break
}
requestAnimationFrame(onFrame)
}) |
Hacky way to get current time, works with paused tracks and doesn't need manual tracking. I didn't test it with stopped or detached tracks tho const isPaused = !audio.playing;
const lastTimePlayed = audio.lastTimePlayed;
const offsetTime = audio.offsetTime || 0;
const contextTime = audio.sourceNode?.context?.currentTime || 0;
const currentTime = isPaused ? offsetTime : contextTime - lastTimePlayed; |
I implemented this, albeit with some changes namely the transport keyword, is replaced by state and some other changes:
const state={type:"stop",playhead:0,timeRef:0} const playbuttonhandler=function(){ const pausebuttonhandler=function(){ const stopbuttonhandler=function(){ requestAnimationFrame(function onFrame() { I really love this library and it is still solid after many years. I am about to replace all .mp3 files with .flac and see how they load but one thing I think Pizzicato.js needs, is a way to be able to click on a timeline and navigate to a particular section of a song since people are so used to being able to do that with stereo recordings, for example on the streaming platforms but it may require the original developer to implement unless there are savvy users who can come up with something brilliant, as is this case with the code originally posted in this thread. |
Thank you for your cool library.
Is there any way for getting the
currentTime
of sound and change it?Something like
audio.currentTime = 5;
?The text was updated successfully, but these errors were encountered: