@threlte/extras

<PositionalAudio>

Creates a positional audio entity. This uses the Web Audio API.

You need to have an <AudioListener> component in your scene in order to use <Audio>and <PositionalAudio>components. The <AudioListener> component needs to be mounted before any <Audio> or <PositionalAudio> components:

<T.PerspectiveCamera makeDefault>
  <AudioListener />
</T.PerspectiveCamera>

<PositionalAudio />
Music: legrisch

Example

<script>
  import { T, Canvas } from '@threlte/core'
  import { AudioListener, PositionalAudio } from '@threlte/extras'
  import Car from './Car.svelte'
</script>

<Canvas>
  <T.PerspectiveCamera
    makeDefault
    position={[3, 3, 3]}
    lookAt={[0, 0, 0]}
  >
    <AudioListener />
  </T.PerspectiveCamera>

  <Car>
    <PositionalAudio
      autostart
      loop
      refDistance={10}
      volume={0.2}
      src={'/audio/car-noise.mp3'}
    />
  </Car>
</Canvas>

Component Signature

<PositionalAudio> extends <T.PositionalAudio> and supports all its props, slot props, bindings and events.

Props

name
type
required
default
description

src
string | AudioBuffer | HTMLMediaElement | AudioBufferSourceNode | MediaStream
yes

autoplay
boolean
no

detune
number
no

directionalCone
{ coneInnerAngle: number, coneOuterAngle: number, coneOuterGain: number }
no

distanceModel
string
no

id
string
no
default
The id of the AudioListener this Audio will be attached to.

loop
boolean
no

maxDistance
number
no

playbackRate
number
no

refDistance
number
no

rolloffFactor
number
no

volume
number
no

Events

name
payload
description

load
AudioBuffer
Fired when the audio has loaded.

progress
ProgressEvent<EventTarget>
Fired when the audio is loading.

error
ErrorEvent
Fired when the audio fails to load.

Bindings

name
type

play
(delay?: number) => Promise<THREE.Audio>

pause
() => THREE.Audio

stop
() => THREE.Audio