@@ -77,7 +77,10 @@ export class Sound {
7777 if ( this . _soundV2 instanceof _WebAudioSoundSource ) {
7878 return ;
7979 }
80- this . _soundV2 . _getOptions ( ) . autoplay = value ;
80+
81+ if ( this . _soundV2 ) {
82+ this . _soundV2 . _getOptions ( ) . autoplay = value ;
83+ }
8184 }
8285
8386 /**
@@ -91,7 +94,10 @@ export class Sound {
9194 if ( this . _soundV2 instanceof _WebAudioSoundSource ) {
9295 return ;
9396 }
94- this . _soundV2 . loop = value ;
97+
98+ if ( this . _soundV2 ) {
99+ this . _soundV2 . loop = value ;
100+ }
95101 }
96102
97103 /**
@@ -108,7 +114,7 @@ export class Sound {
108114 * Is this sound currently played.
109115 */
110116 public get isPlaying ( ) : boolean {
111- return this . _soundV2 instanceof _WebAudioSoundSource ? true : this . _soundV2 . state === SoundState . Started || this . _optionsV2 . autoplay ! ;
117+ return this . _soundV2 instanceof _WebAudioSoundSource ? true : this . _soundV2 ? .state === SoundState . Started || this . _optionsV2 . autoplay ! ;
112118 }
113119
114120 /**
@@ -137,7 +143,10 @@ export class Sound {
137143 }
138144 public set maxDistance ( value : number ) {
139145 this . _optionsV2 . spatialMaxDistance = value ;
140- this . _soundV2 . spatial . maxDistance = value ;
146+
147+ if ( this . _soundV2 ) {
148+ this . _soundV2 . spatial . maxDistance = value ;
149+ }
141150 }
142151 /**
143152 * Define the distance attenuation model the sound will follow.
@@ -148,7 +157,10 @@ export class Sound {
148157 }
149158 public set distanceModel ( value : "linear" | "inverse" | "exponential" ) {
150159 this . _optionsV2 . spatialDistanceModel = value ;
151- this . _soundV2 . spatial . distanceModel = value ;
160+
161+ if ( this . _soundV2 ) {
162+ this . _soundV2 . spatial . distanceModel = value ;
163+ }
152164 }
153165 /**
154166 * @internal
@@ -177,15 +189,17 @@ export class Sound {
177189 * @see https://doc.babylonjs.com/legacy/audio#creating-a-spatial-3d-sound
178190 */
179191 public get spatialSound ( ) : boolean {
180- return this . _soundV2 . _isSpatial ;
192+ return this . _soundV2 ? ._isSpatial ?? false ;
181193 }
182194
183195 /**
184196 * Does this sound enables spatial sound.
185197 * @see https://doc.babylonjs.com/legacy/audio#creating-a-spatial-3d-sound
186198 */
187199 public set spatialSound ( newValue : boolean ) {
188- this . _soundV2 . _isSpatial = newValue ;
200+ if ( this . _soundV2 ) {
201+ this . _soundV2 . _isSpatial = newValue ;
202+ }
189203 }
190204
191205 private _localDirection : Vector3 = new Vector3 ( 1 , 0 , 0 ) ;
@@ -279,10 +293,17 @@ export class Sound {
279293 optionsV2 . spatialRotationQuaternion = _SpatialAudioDefaults . rotationQuaternion ;
280294 }
281295
296+ this . _optionsV2 = optionsV2 ;
297+
282298 this . useCustomAttenuation = options . useCustomAttenuation ?? false ;
283299
284300 let streaming = options ?. streaming || false ;
285301
302+ const audioEngine = AbstractEngine . audioEngine ;
303+ if ( ! audioEngine ) {
304+ return ;
305+ }
306+
286307 const audioEngineV2 = ( AbstractEngine . audioEngine as AudioEngine ) . _v2 ;
287308
288309 const createSoundV2 = ( ) => {
@@ -336,8 +357,6 @@ export class Sound {
336357 this . _soundV2 = createSoundV2 ( ) ;
337358 }
338359
339- this . _optionsV2 = optionsV2 ;
340-
341360 if ( ! this . _soundV2 ) {
342361 Logger . Error ( "Parameter must be a URL to the sound, an Array of URLs (.mp3 & .ogg) or an ArrayBuffer of the sound." ) ;
343362 return ;
@@ -692,6 +711,10 @@ export class Sound {
692711 * @param time (optional) Stop the sound after X seconds. Stop immediately (0) by default.
693712 */
694713 public stop ( time ?: number ) : void {
714+ if ( ! this . _soundV2 ) {
715+ return ;
716+ }
717+
695718 // WebAudio sound sources have no `stop` function because they are always playing.
696719 if ( this . _soundV2 instanceof _WebAudioSoundSource ) {
697720 return ;
@@ -708,6 +731,10 @@ export class Sound {
708731 * Put the sound in pause
709732 */
710733 public pause ( ) : void {
734+ if ( ! this . _soundV2 ) {
735+ return ;
736+ }
737+
711738 // WebAudio sound sources have no `pause` function because they are always playing.
712739 if ( this . _soundV2 instanceof _WebAudioSoundSource ) {
713740 return ;
0 commit comments