[Studio] world.audio.addSound() no longer exists?

Hi there,

Our Studio project has just started throwing an error: Unhandled promise rejection: TypeError: world.audio.addSound is not a function

This function seemed to be working fine before but now cannot be used - I checked the docs to see if it has been deprecated or replaced but they still point towards using addSound(). We’ve commented out the calls for now and the project’s now working (albeit without sound) - I was wondering if perhaps there was an alternative we should be using?

Cheers,
Dylan

Thanks for pointing this out! I’ll make sure our docs get updated.

The methods world.audio.(mute/unmute/play/pause/setVolume) are still supported, but audio should now be added to an entity using the audio component.

Thanks for the update George!

Just another question for you - once I’ve made an entity with an audio component, how can I fire off a singular sound effect? Music is easy enough by setting loop/autoplay to true, but I’m unsure on how to use a function like world.audio.play() to activate an entity’s audio component. Looking at the docs, it doesn’t seem like the component itself has a way to play its corresponding audio file, so I’m a bit lost on where to look next. Perhaps I need to add the audio components to entities at runtime?

The most effective way to play a one-time sound effect is to create an entity with an Audio component specifying the src. Then, use a timeout set to the duration of the sound effect to delete the entity once the sound finishes playing.

I gave it a try - works great for looping background music which is created during my audio manager’s add() call.

I have this function which I’m using for creating one-time SFX, which is creating a new entity for each instance as you recommended. I’ve encountered a few issues with it. I’ve also pasted the function I use for looping music (which works great) as a comparison. In both cases, the new entity is added as a child of the audio manager object for organisation’s sake (hence the use of a singleton EID).

The one-time SFX don’t seem to play unless i set loop: true in the ecs.Audio.set()
call - I’m not sure of another way to get the audio to play automatically at this stage. I thought that maybe letting the one-time SFX loop is fine, since I also have the timeout function to remove the entity after its first playthrough, the time of which is an ecs.f32 variable, but for some reason the timeout function causes playback of SFX to be very inconsistent. Sometimes the sound effect plays, sometimes it’s cut off prematurely, and most often, they simply don’t play at all.

Any advice? I wonder if I’m missing something in my function.

function playOneTimeSFX(world, url, time, volume) {
  if (singletonEID === null) return
  const urlPrefix = 'assets/Audio/'
  const entity = world.createEntity()
  world.setParent(entity, singletonEID)
  ecs.Audio.set(world, entity, {
    url: `${urlPrefix}${url}`,
    volume,
    pitch: 1,
  })

  world.time.setTimeout(() => {
    world.deleteEntity(entity)
  }, time)
}

function startLoopedMusic(world, url, volume) {
  if (singletonEID === null) return

  const urlPrefix = 'assets/Audio/'
  const entity = world.createEntity()
  world.setParent(entity, singletonEID)
  ecs.Audio.set(world, entity, {
    url: `${urlPrefix}${url}`,
    volume,
    pitch: 1,
    loop: true,
  })
}

Weird! Could it be that singletonEID isn’t coming through sometimes and that’s causing strange behavior?

The singletonEID seems to come through consistently - I tried removing any references to it to see if that would have an effect, but still getting nothing from the SFX. The entities are definitely being created & eventually removed, and the time before removal is long after the duration of the sound itself. The only way I’ve been able to get the SFX to actually sound off is when I set loop: true, but then I get the issues I mentioned before. I saw that there’s an autoplay option in the Audio component but I don’t see a way to set it in code - I was wondering if there’s possibly a way to do that?

Autoplay sets playing to be true on the component. Weird that it only works with loop. Could it be that the Audio takes a bit to load each time? Can you try delaying the removal of the entity by a second or two and see that works?

Figured it out! Bit of a silly move on my part - I realised the time variables for deleting the audio components are supposed to be in milliseconds. I was using seconds :sweat_smile: Got it working now, thanks for all your help!

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.