Start native apps faster with the Composables CLI ->
Function

spatializedAudioOutput

Adds spatialized audio output to this Composable.

SoundEffectPlayerSample

/** A sample demonstrating a spatialized sound effect player. */
@Composable
fun SoundEffectPlayerSample() {
    val context = LocalContext.current
    val session = LocalSession.current ?: return
    // Create a SoundEffectPool.
    val soundEffectPool = remember { SoundEffectPool.create(session, maxStreams = 10) }
    // Remember a sound effect loaded via AssetFileDescriptor.
    val soundEffect = remember {
        val assetFileDescriptor = context.assets.openFd("click.wav")
        soundEffectPool.load(assetFileDescriptor)
    }
    // Create a SoundEffectPoolComponent.
    val soundEffectPoolComponent =
        remember(session, soundEffectPool) {
            SoundEffectPoolComponent.create(session, soundEffectPool, PointSourceParams())
        }
    // Attach the SpatializedAudioOutput via SubspaceModifier.spatializedAudioOutput.
    SpatialPanel(
        modifier =
            SubspaceModifier.width(600.dp)
                .height(400.dp)
                .spatializedAudioOutput(soundEffectPoolComponent.asSpatializedAudioOutput())
                .movable()
    ) {
        Button(
            onClick = {
                soundEffectPoolComponent.play(
                    soundEffect,
                    volume = 1f,
                    priority = 0,
                    isLooping = false,
                )
            }
        ) {
            Text("Play Sound Effect")
        }
    }
}

SoundFieldSpatializedAudioOutputSample

/** A sample demonstrating a sound field spatialized audio output with ExoPlayer. */
@Composable
fun SoundFieldSpatializedAudioOutputSample() {
    val context = LocalContext.current
    val session = LocalSession.current ?: return
    // Create a SoundFieldExoplayerAudioOutput.
    val audioOutput = remember {
        SoundFieldExoplayerAudioOutput(
            session,
            SoundFieldAttributes(SpatializerConstants.AmbisonicsOrder.FIRST_ORDER),
        )
    }
    // Create an ExoPlayer and set its AudioOutputProvider.
    val exoPlayer = remember {
        ExoPlayer.Builder(context).setAudioOutputProvider(audioOutput.audioOutputProvider).build()
    }
    DisposableEffect(Unit) {
        exoPlayer.setMediaItem(MediaItem.fromUri("asset:///ambient.mp3"))
        exoPlayer.prepare()
        exoPlayer.play()
        onDispose { exoPlayer.release() }
    }
    // Attach the SpatializedAudioOutput via SubspaceModifier.spatializedAudioOutput.
    SpatialPanel(
        modifier =
            SubspaceModifier.width(600.dp)
                .height(400.dp)
                .spatializedAudioOutput(audioOutput)
                .movable()
    ) {
        // Content of the panel
    }
}

SpatializedAudioOutputSample

/** A sample demonstrating a point source spatialized audio output with ExoPlayer. */
@Composable
fun SpatializedAudioOutputSample() {
    val context = LocalContext.current
    // Create a PointSourceExoplayerAudioOutput.
    val audioOutput = rememberPointSourceExoplayerAudioOutput(PointSourceParams())
    // Create an ExoPlayer and set its AudioOutputProvider.
    val exoPlayer = remember {
        ExoPlayer.Builder(context).setAudioOutputProvider(audioOutput.audioOutputProvider).build()
    }
    DisposableEffect(Unit) {
        exoPlayer.setMediaItem(MediaItem.fromUri("asset:///audio.mp3"))
        exoPlayer.prepare()
        exoPlayer.play()
        onDispose { exoPlayer.release() }
    }
    // Attach the SpatializedAudioOutput via SubspaceModifier.spatializedAudioOutput. The audio will
    // be spatialized from the position of this SpatialPanel, and will follow the Panel as it moves.
    SpatialPanel(
        modifier =
            SubspaceModifier.width(600.dp)
                .height(400.dp)
                .spatializedAudioOutput(audioOutput)
                .movable()
    ) {
        // Content of the panel
    }
}

Last updated: