# SoundEffectPool

Artifact: [androidx.xr.scenecore:scenecore](https://developer.android.com/jetpack/androidx/releases/xr-scenecore) [View Source](https://cs.android.com/search?q=file:androidx/xr/scenecore/SoundEffectPool.kt+class:androidx.xr.scenecore.SoundEffectPool) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.0.0)

*** ** * ** ***


```
public final class SoundEffectPool implements AutoCloseable
```

<br />

*** ** * ** ***

Manages the loading and unloading of sound assets into memory.

If the value of the `https://developer.android.com/reference/androidx/xr/scenecore/SoundEffectPool#(androidx.xr.scenecore.runtime.SceneRuntime,kotlin.Int)` parameter exceeds the capabilities for the platform then the value will be clamped to the platform's max number of supported streams.

## Summary

| ### Nested types |
|---|
| `public fun interface https://developer.android.com/reference/androidx/xr/scenecore/SoundEffectPool.LoadCompleteListener` Callback interface for receiving notification when a sound effect has finished loading. |

| ### Public methods |
|---|---|
| `final void` | `https://developer.android.com/reference/androidx/xr/scenecore/SoundEffectPool#addLoadCompleteListener(androidx.xr.scenecore.SoundEffectPool.LoadCompleteListener)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/SoundEffectPool.LoadCompleteListener listener )` Adds a listener to be invoked when sounds finish loading. |
| `final void` | `https://developer.android.com/reference/androidx/xr/scenecore/SoundEffectPool#addLoadCompleteListener(java.util.concurrent.Executor,androidx.xr.scenecore.SoundEffectPool.LoadCompleteListener)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/concurrent/Executor.html executor, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/SoundEffectPool.LoadCompleteListener listener )` Adds a listener to be invoked when sounds finish loading. |
| `void` | `https://developer.android.com/reference/androidx/xr/scenecore/SoundEffectPool#close()()` |
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/SoundEffectPool` | `https://developer.android.com/reference/androidx/xr/scenecore/SoundEffectPool#create(androidx.xr.runtime.Session,kotlin.Int)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/Session session, int maxStreams)` Creates a `https://developer.android.com/reference/androidx/xr/scenecore/SoundEffectPool` with the given `https://developer.android.com/reference/androidx/xr/scenecore/SoundEffectPool.Companion#create(androidx.xr.runtime.Session,kotlin.Int)` and `https://developer.android.com/reference/androidx/xr/scenecore/SoundEffectPool.Companion#create(androidx.xr.runtime.Session,kotlin.Int)`. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/SoundEffect` | `https://developer.android.com/reference/androidx/xr/scenecore/SoundEffectPool#load(android.content.res.AssetFileDescriptor)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/res/AssetFileDescriptor.html assetFileDescriptor)` Loads a sound from an `https://developer.android.com/reference/android/content/res/AssetFileDescriptor.html` and returns a `https://developer.android.com/reference/androidx/xr/scenecore/SoundEffect` handle. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/SoundEffect` | `https://developer.android.com/reference/androidx/xr/scenecore/SoundEffectPool#load(android.content.Context,kotlin.Int)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context, int resId)` Loads a sound from an application resource and returns a `https://developer.android.com/reference/androidx/xr/scenecore/SoundEffect` handle. |
| `final void` | `https://developer.android.com/reference/androidx/xr/scenecore/SoundEffectPool#release()()` Releases all native resources associated with this pool. |
| `final void` | `https://developer.android.com/reference/androidx/xr/scenecore/SoundEffectPool#removeLoadCompleteListener(androidx.xr.scenecore.SoundEffectPool.LoadCompleteListener)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/SoundEffectPool.LoadCompleteListener listener )` Removes a listener previously added via `https://developer.android.com/reference/androidx/xr/scenecore/SoundEffectPool#addLoadCompleteListener(androidx.xr.scenecore.SoundEffectPool.LoadCompleteListener)` |
| `final boolean` | `https://developer.android.com/reference/androidx/xr/scenecore/SoundEffectPool#unload(androidx.xr.scenecore.SoundEffect)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/SoundEffect soundEffect)` Unloads a sound from memory to release resources. |

## Public methods

### addLoadCompleteListener

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.0.0)

```
public final void addLoadCompleteListener(
    @NonNull SoundEffectPool.LoadCompleteListener listener
)
```

Adds a listener to be invoked when sounds finish loading. The listener will be called on the main thread.

### addLoadCompleteListener

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.0.0)

```
public final void addLoadCompleteListener(
    @NonNull Executor executor,
    @NonNull SoundEffectPool.LoadCompleteListener listener
)
```

Adds a listener to be invoked when sounds finish loading. The listener will be called on the given `https://developer.android.com/reference/java/util/concurrent/Executor.html`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/concurrent/Executor.html executor` | The `https://developer.android.com/reference/java/util/concurrent/Executor.html` to run the listener on. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/SoundEffectPool.LoadCompleteListener listener` | The `https://developer.android.com/reference/androidx/xr/scenecore/SoundEffectPool.LoadCompleteListener` to be invoked asynchronously on the given executor. |

### close

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.0.0)

```
public void close()
```

### create

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.0.0)

```
public static final @NonNull SoundEffectPool create(@NonNull Session session, int maxStreams)
```

Creates a `https://developer.android.com/reference/androidx/xr/scenecore/SoundEffectPool` with the given `https://developer.android.com/reference/androidx/xr/scenecore/SoundEffectPool.Companion#create(androidx.xr.runtime.Session,kotlin.Int)` and `https://developer.android.com/reference/androidx/xr/scenecore/SoundEffectPool.Companion#create(androidx.xr.runtime.Session,kotlin.Int)`.

If the value of the `https://developer.android.com/reference/androidx/xr/scenecore/SoundEffectPool.Companion#create(androidx.xr.runtime.Session,kotlin.Int)` parameter exceeds the capabilities for the platform then the value will be clamped to the platform's max number of supported streams.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/runtime/Session session` | the XR session associated with this sound effect pool |
| `int maxStreams` | maximum number of simultaneous streams that can be played by this pool |

### load

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.0.0)

```
public final @NonNull SoundEffect load(@NonNull AssetFileDescriptor assetFileDescriptor)
```

Loads a sound from an `https://developer.android.com/reference/android/content/res/AssetFileDescriptor.html` and returns a `https://developer.android.com/reference/androidx/xr/scenecore/SoundEffect` handle.

Note: Loading is asynchronous. The sound may not be ready to play immediately after this method returns. Use `https://developer.android.com/reference/androidx/xr/scenecore/SoundEffectPool.LoadCompleteListener` to be notified when it is ready.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/res/AssetFileDescriptor.html assetFileDescriptor` | the `https://developer.android.com/reference/android/content/res/AssetFileDescriptor.html` for the sound file |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/SoundEffect` | a `https://developer.android.com/reference/androidx/xr/scenecore/SoundEffect` handle for the loaded sound |

### load

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.0.0)

```
public final @NonNull SoundEffect load(@NonNull Context context, int resId)
```

Loads a sound from an application resource and returns a `https://developer.android.com/reference/androidx/xr/scenecore/SoundEffect` handle.

Note: Loading is asynchronous. The sound may not be ready to play immediately after this method returns. Use `https://developer.android.com/reference/androidx/xr/scenecore/SoundEffectPool.LoadCompleteListener` to be notified when it is ready.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context` | the application context |
| `int resId` | resource ID of the sound (e.g., R.raw.sound) |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/SoundEffect` | a `https://developer.android.com/reference/androidx/xr/scenecore/SoundEffect` handle for the loaded sound |

### release

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.0.0)

```
public final void release()
```

Releases all native resources associated with this pool.

### removeLoadCompleteListener

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.0.0)

```
public final void removeLoadCompleteListener(
    @NonNull SoundEffectPool.LoadCompleteListener listener
)
```

Removes a listener previously added via `https://developer.android.com/reference/androidx/xr/scenecore/SoundEffectPool#addLoadCompleteListener(androidx.xr.scenecore.SoundEffectPool.LoadCompleteListener)`

### unload

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/xr-scenecore#1.0.0)

```
public final boolean unload(@NonNull SoundEffect soundEffect)
```

Unloads a sound from memory to release resources.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/xr/scenecore/SoundEffect soundEffect` | the `https://developer.android.com/reference/androidx/xr/scenecore/SoundEffect` to unload |

| Returns |
|---|---|
| `boolean` | true if the sound was successfully unloaded |