Skip to content
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

Missing a way to set the start time of sound components #5364

Closed
JonathannJacobs opened this issue Oct 18, 2023 · 4 comments
Closed

Missing a way to set the start time of sound components #5364

JonathannJacobs opened this issue Oct 18, 2023 · 4 comments

Comments

@JonathannJacobs
Copy link
Contributor

Hi, well first thing, it's not a bug but a missing feature.

I wish to have a way to play a sound entirely at first loop and then rewind at some specific start loop value different than zero.
So far i had the following code :

this.el.removeAttribute('sound')
this.el.setAttribute('sound', 'src', 'assets/some.mp3');
this.el.components.sound.playSound()
this.el.addEventListener('sound-ended', () => {
// Missing here a way to set the next playSound start value
this.el.components.sound.playSound()
});

And that is sad because i could totally do it with a regular audio html element that lacks positionnal audio and pool.

Currently my best shot is to split my file into two mp3: one for the intro and another for the looping part.

  • A-Frame Version: 1.4.2
  • Platform / Device: All
@JonathannJacobs JonathannJacobs changed the title Missing a way to set the time start time of sound components Missing a way to set the start time of sound components Oct 18, 2023
@mrxz
Copy link
Contributor

mrxz commented Oct 18, 2023

It would be nice for the built-in sound component to expose a loopStart and loopEnd, but currently it doesn't. Luckily you can quite easily reach into the sound component and get to the THREE.Audio instance or even further to the underlying WebAudio Nodes.

For example:

const soundComponent = this.el.components['sound'];
const audio = soundComponent.pool.children[0]; // THREE.Audio
audio.setLoopStart(/* start time in seconds */);
audio.setLoopEnd(audio.buffer.duration)

It can be a bit tricky when to execute this, as it needs to take place after the sound component has initialized (and the sound loaded, for the duration), but before playing the audio (otherwise the loopStart and loopEnd of THREE.Audio don't apply). Should be good to handle it in sound-loaded: this.el.addEventListener('sound-loaded', e => {})

@JonathannJacobs
Copy link
Contributor Author

Indeed, that's usefull info. Thank you
I also found out that playSound does take a callback for preprocessing sounds which can be used for this purpose, but there is also an issue here : if the sound isn't loaded yet, mustPlay will be set to true and function will return immediatly, forgetting to save whatever callback we did set. And this is a bug actually.

JonathannJacobs added a commit to JonathannJacobs/aframe that referenced this issue Oct 18, 2023
… before the sound was loaded will be called when loaded and not ignored anymore.
@JonathannJacobs
Copy link
Contributor Author

JonathannJacobs commented Oct 18, 2023

Wrote a fix and commited it. If it is accepted here should be the solution to my issue :
this.el.removeAttribute('sound')
this.el.setAttribute('sound', 'src', 'assets/some.mp3');
this.el.components.sound.playSound((snd) => {
snd.setLoop(true)
snd.setLoopStart(playbackTime) // set your loop restart time here, in seconds
snd.setLoopEnd(snd.buffer.duration) // Up to the end or else
})

JonathannJacobs added a commit to JonathannJacobs/aframe that referenced this issue Oct 18, 2023
JonathannJacobs added a commit to JonathannJacobs/aframe that referenced this issue Oct 19, 2023
…the sound if a loop start was specified without an end
JonathannJacobs added a commit to JonathannJacobs/aframe that referenced this issue Oct 19, 2023
dmarcos pushed a commit that referenced this issue Oct 23, 2023
…ed + mustPlay (#5365)

* #5364 Bugfix : The processSound callback given to playSound() before the sound was loaded will be called when loaded and not ignored anymore.

* #5364 : Sound : Added API for reaching THREE.Audio.loopStart() and loopEnd()

* #5364 : Sound : Automatically set the loop end to the end of the sound if a loop start was specified without an end

* #5364 : Sound : Code review changes applied.
@dmarcos
Copy link
Member

dmarcos commented Oct 23, 2023

Fixed by #5365

@dmarcos dmarcos closed this as completed Oct 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants