Page 1 of 2
playsound
Posted: 11 June 2024, 13:53
by AxeCrazy
G'day,
I am in progress of developing Sirens, a game of composing a "song" by laying cards in a certain order.
One of the option i like to add is after all cards are placed is to play the "song" by playing all parts (which are all seperate mp3's) in the order as the cards are placed, but the sample do not have exactly the same length.
Now i start the next sample after 3 seconds, which is more or less the length of the samples via a settimeout function, but it would be better to get an event after playsound is completed so i can start the next one.
Does playsound have such an option (undocument then i guess) or should i set the timeout variabel per card?
Any thoughts?
Re: playsound
Posted: 11 June 2024, 15:08
by Jonathan2004
Hi,
I don't have knowledge of BGA sounds for now, but when I read your message I thought "What if you save all different combinations as different bigger sound files ?" (not loaded by default). It will solve the problem of "holes" between sounds.
Of course it depends on the number of combinations...
Re: playsound
Posted: 11 June 2024, 22:46
by AxeCrazy
since there are 18 different cards that can be placed that would result in 18! options, which is a little too much

think i will go for determining the exact length of the seperate mp3 and use a variable settimeout delay.
Which is not even so evident to determine btw
But thanks for your reply
Re: playsound
Posted: 12 June 2024, 10:32
by KuWizard
Have never solved this problem before but maybe there is a way to programmatically "glue" multiple mp3s to a single mp3 to play it later?
However from my experience of using sounds on BGA filenaames need to be hardcoded so that is probably not the option

Re: playsound
Posted: 13 June 2024, 11:29
by jonas9731
I am an experienced JavaScript game developer that have not (yet) worked on any BGA projects. This would be easy using JavaScript, just adding an ended eventListener to the sound would do the trick.
It is also possible to get the duration of any soundfile using JavaScript, but I ould prefer the solution above.
Sounds like a really cool project btw, I would be happy to help!
Re: playsound
Posted: 14 June 2024, 15:20
by AxeCrazy
That was indeed my idea, but the playsound function does not seem to have an option to attach an eventlistener according to the documentation available.
I did timeout based on the length of the sample now and that works.
Re: playsound
Posted: 14 June 2024, 16:08
by quietmint
I would recommend the event listener method. Although the sound file should be preloaded/cached, this isn't guaranteed. Even if the file is fully downloaded, you don't know exactly how long it will take the browser to prepare and play the sound. If you use BGA's playSound() method, you still need to create your own <audio> tags somehow. Presumably you have a separate <audio> tag for each of the 18 sounds, either hard-coded in your .tpl file or created by JavaScript during your setup() function. You can add a common event listener for all those <audio> tags in your setup() function.
When it's time to play a sound (sequence), populate some array "playlist" with the IDs of the sounds needed, in order.
var playlist = ["mygame_sound1", "mygame_sound2", "mygame_sound2", "mygame_sound1"];
In the event listener, when the current sound stops playing, check if the playlist array isn't empty and if the first item matches this sound. If yes, shift this item off the array and call playSound() on the new first item.
Re: playsound
Posted: 14 June 2024, 17:27
by AxeCrazy
but how can i attach a eventlistener to the playsound event supported in BGA?
It is not documented, so?
Re: playsound
Posted: 14 June 2024, 19:12
by quietmint
Playsound function just triggers the audio tag to play. Event listeners function as normal (provided by the browser, nothing special about BGA). Event listener is attached to the audio tag generally, invokes each time the audio stops playing (not just this specific invocation of playsound function)
Re: playsound
Posted: 15 June 2024, 09:32
by jonas9731
AxeCrazy wrote: ↑14 June 2024, 17:27
but how can i attach a eventlistener to the playsound event supported in BGA?
It is not documented, so?
I have not started developing within BGA, my ideas stem from my general experience with JavaScript game development. I hope to get started with BGA dev soon.