Skip to content

Instantly share code, notes, and snippets.

@skang2-sc
Last active April 4, 2025 03:11
Show Gist options
  • Save skang2-sc/b294c39c77fd29c4eaa69f39332cf4d8 to your computer and use it in GitHub Desktop.
Save skang2-sc/b294c39c77fd29c4eaa69f39332cf4d8 to your computer and use it in GitHub Desktop.
Will help you seek to a specific position in your audio component in Lens Studio
@component
export class AudioHelper extends BaseScriptComponent {
// Private property to hold a reference to the AudioComponent
private audioComponent: AudioComponent;
// Called when the script is initialized
onAwake() {
// Retrieve the AudioComponent attached to the same SceneObject
this.audioComponent = this.sceneObject.getComponent("AudioComponent");
// Call the testSeek method to set up a test event
this.testSeek();
}
// Method to seek the audio playback to a specific time
seek(playbackTimeInSeconds: number) {
// Clamp the playback time to ensure it is within the valid range
playbackTimeInSeconds = MathUtils.clamp(
playbackTimeInSeconds,
0,
this.audioComponent.duration
);
// Set the audio playback position to the clamped time
this.audioComponent.position = playbackTimeInSeconds;
}
// Method to test the seek functionality
testSeek() {
// Create a TapEvent and bind a callback function to it
this.createEvent("TapEvent").bind(() => {
// Seek the audio to 60 seconds
this.seek(60);
// Play the audio from the new position only playing once.
this.audioComponent.play(1);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment