Sound
Playing sound
import { inject, Service, World, GameInstance, ViewController, AssetLoader } from '@hology/core/gameplay';
import * as THREE from 'three';
@Service()
class Game extends GameInstance {
private world = inject(World)
private view = inject(ViewController)
private assetLoader = inject(AssetLoader)
// 1
private sound = new THREE.Audio(this.view.audioListener);
async onStart() {
// 2
const buffer = await this.assetLoader.getAudioAtPath('data/asset-resources/impactWood_heavy_001.ogg')
// 3
this.sound.setBuffer(buffer).setVolume(0.5)
}
playSound() {
if (this.sound.isPlaying) {
this.sound.stop()
}
// 4
this.sound.play();
}
}
export default Game