AudioSource
Namespace:
namespace Lenga\Engine\Core;
class AudioSource
AudioSource plays an audio clip from a GameObject.
Use it for background music, UI sounds, pickup sounds, impacts, jump sounds,
and other one-shot or looping audio. In the editor, add an Audio Source
component, assign an audio clip, then set playback options such as Play On Awake, Loop, Volume, and Pitch.
Properties
clipPath
public string $clipPath
Project-relative path to the audio file this source should play.
Example:
$audio->clipPath = 'Assets/Audio/jump.wav';
playOnAwake
public bool $playOnAwake
When true, the source starts playing automatically when the component starts.
When false, the clip only plays when you call play() from code.
loop
public bool $loop
When true, the source restarts after the clip finishes. Use this for
background music and ambience.
volume
public float $volume
Playback volume from 0.0 to 1.0.
pitch
public float $pitch
Playback pitch. 1.0 is the normal pitch. Values above 1.0 play higher and
faster. Values below 1.0 play lower and slower.
Methods
play
public function play(): bool
Starts the assigned clip. Returns false if the clip cannot be played.
stop
public function stop(): bool
Stops playback.
isPlaying
public function isPlaying(): bool
Returns whether this source is currently playing.
Example: Play a Jump Sound
use Lenga\Engine\Core\Behaviour;
use Lenga\Engine\Core\AudioSource;
use Lenga\Engine\Core\Input;
use Lenga\Engine\Core\KeyCode;
class SoundEffectPlayer extends Behaviour
{
public function start(): void
{
$audio = $this->gameObject->getComponent(AudioSource::class);
$audio->clipPath = 'assets/sounds/jump.wav';
$audio->volume = 0.8;
}
public function update(): void
{
if (Input::getKeyDown(KeyCode::SPACE)) {
$audio = $this->gameObject->getComponent(AudioSource::class);
if (!$audio->isPlaying()) {
$audio->play();
}
}
}
}
Notes
Play On Awakeonly controls automatic playback. A script can still callplay()explicitly.- Active audio sources pause and resume with the engine-wide pause state.
- Use looping sources for music and ambience. Use explicit
play()calls for short gameplay sounds.