Hello,
I have some questions regarding ecs.Audio
please.
(1)
I have an entity called GameOverScreen with a custom component responsible for playing game-over audio when certain conditions are met. If the game over happens quickly after starting the game, the audio plays as expected. However, if it takes some time before the game over occurs, the audio doesn’t play. I’m not sure why or if there is anything I should check which could be the source of this problem?
Here’s my code:
In the Game Over custom component’s add method, I create an audio element:
add: (world, component) => {
const {eid, schema: {landingScreenId, gameManagerId}} = component
ecs.Audio.set(world, eid, {
url: gameOverAudioSrc,
loop: false,
paused: true, // not playing by default
})
...
const handleGameOver = () => {
const audioGameOver = ecs.Audio.cursor(world, eid)
audioGameOver.paused = false
}
world.events.addListener(eid, 'game-over', handleGameOver)
Initially, I tried creating the audio component inside the handleGameOver callback using ecs.Audio.set
, but I faced the same issue. So I’m testing alternatives where I set it to paused: true by default and unpause it later.
I even tried to set the volume to 0 initially with pause: false
but the audio starts and I can hear it, it’s like volume 0 is not working.
I also tried changing pause to true with an ecs.Audio.acquire
and ecs.Audio.commit
but I still have the same issue.
const handleGameOver() => {
...
ecs.Audio.set(world, eid, {
url: gameOverAudioSrc,
loop: false,
paused: false,
// volume: 0,
})
}
(2)
Ideally, I would like to set the audio component directly on the entity from the Studio UI and by code play it when the ‘game-over’ event is triggered, but I haven’t figured out how to do this yet. Although I think in some use case it’s interesting to understand how to do it by code.
Thank you.