-
Notifications
You must be signed in to change notification settings - Fork 3
API
marchell edited this page Aug 2, 2025
·
4 revisions
Playing audio clips is simple. All you need is your own AudioHandler instance.
using LabExtended.API.Audio;
public void CreateHandler()
{
// Retrieves our AudioHandler (if we've already created one)
// OR creates a new one (in which case the delegate in the second parameter is called)
AudioHandler handler = AudioManager.GetOrAddHandler("ExampleHandler", handler =>
{
// Adds an AudioPlayer instance to your AudioHandler instance.
handler.AddPlayer();
// Adds a SpeakerToy instance to your AudioHandler instance.
// The first parameter is the name of the SpeakerToy, used only in the AudioHandler.
// The second parameter is the controller ID of the SpeakerToy, which is used when sending audio to players.
// - Assigning the same ID to multiple speakers results in the same audio playing from all speakers with this ID.
handler.EnsureSpeaker("ExampleSpeaker", 1);
});
// Lets also load the clip that we want to play.
// It's important to use the same name as the file in the method's first argument (this includes any extensions).
KeyValuePair<string, byte[]> clip = AudioManager.GetClip("ExampleClip");
}// This is called every time a player sends a voice chat packet to the server.
public void OnSpeaking(PlayerSendingVoiceChatMessageEventArgs args)
{
// You should save your AudioHandler instance as a field or a property, this event is called A LOT
if (!AudioManager.TryGetHandler("ExampleHandler", out var handler))
return;
// Since this is a LabAPI events and not a custom LabExtended event, we need to cast the LabAPI player instance
// into our custom subtype ExPlayer instance.
if (args.Speaker is not ExPlayer speaker)
return;
// This will send the voice chat message through each speaker you defined in your AudioHandler.
handler.Transmit(args.Messsage.Data, args.Message.DataLength);
// This will send the voice chat message through each speaker to players that are within 20 meters of the speaking player.
handler.Transmit(args.Messsage.Data, args.Message.DataLength, player => player.Position.DistanceTo(speaker) <= 20f);
}public void PlayClip()
{
// Will return false if no AudioHandler with name "ExampleHandler" is found.
if (!AudioManager.TryGetHandler("ExampleHandler", out var handler))
return;
// Will return false if no audio clip file with name "ExampleClip" is found.
if (!AudioManager.TryGetClip("ExampleClip", out var clip))
return;
// Will start playing the audio clip (or put it into the queue if something is playing)
// Audio of the clip will be sent through each speaker ID defined in your AudioHandler.
handler.Player.Play(clip, false);
// You can also start playback of an audio clip at a specific location.
handler.PlayAt(clip, Vector3.zero);
// OR you can start playback of an audio clip with a specific Transform!
handler.PlayOn(clip, ExPlayer.Players.RandomItem().CameraTransform);
}You can keep track of your playback using events defined in the AudioPlayer class.
This wiki may be incomplete or outdated! Sorry for that, but this project is mostly a one-man show.