Skip to content

API Usage

samyyc edited this page Jan 25, 2025 · 5 revisions

Prerequisites

Metamod

  1. Include the iaudio.h in public folder.
  2. Query the interface in AllPluginsLoaded and use it like this:
// in AllPluginsLoaded
IAudio *pAudio = (IAudio*)g_SMAPI->MetaFactory(AUDIO_INTERFACE, nullptr, nullptr);

// wherever you like
std::string str = "D:/xxx.mp3";
pAudio->PlayFromFile(str, 1.0f);

CounterStrikeSharp

  1. Copy the Audio.cs in public folder to your project.
  2. Use the method in Audio class.

Swiftly

  1. Download swext_audio_windows or swext_audio_linux in releases.
  2. Decompress it and put the .dll or .so file in swiftly/extensions/(platform)/ folder.
  3. Initialize it like this:
local audio = Audio()
audio:PlayFromFile("D:/xxx.mp3", 1.0)

API Methods

SetPlayerHearing

Enable / disable the audio to a specific player, the audio still plays even if they can't hear it.

Params Type Description
slot int player slot
hearing boolean hearing or not

Metamod Usage

pAudio->SetPlayerHearing(slot, hearing);

CounterStrikeSharp Usage

Audio.SetPlayerHearing(slot, hearing);

Swiftly Usage

audio:SetPlayerHearing(slot, hearing)

SetAllPlayerHearing

Enable / disable the audio to all players, the audio still plays even if they can't hear it.

Params Type Description
hearing boolean hearing or not

Metamod Usage

pAudio->SetAllHearing(hearing);

CounterStrikeSharp Usage

Audio.SetAllHearing(hearing);

Swiftly Usage

audio:SetAllHearing(hearing)

IsHearing

Return if a player is hearing or not.

Params Type Description
slot int player slot
Return Type Description
hearing boolean hearing or not

Metamod Usage

auto hearing = pAudio->IsHearing(slot);

CounterStrikeSharp Usage

var hearing = Audio.IsHearing(slot);

Swiftly Usage

local hearing = audio:IsHearing(slot)

PlayToPlayerFromBuffer

Play an audio buffer to specific player. The audio buffer can be in any format as long as it can be recognized by ffmpeg.

Params Type Description
slot int player slot
audioBuffer std::string or string audio buffer
volume float default 1.0

Metamod Usage

pAudio->PlayToPlayerFromBuffer(slot, audioBuffer, volume);

CounterStrikeSharp Usage

Audio.PlayToPlayerFromBuffer(slot, audioBuffer, volume);

Swiftly Usage

audio:PlayToPlayerFromBuffer(slot, audioBuffer, volume)

PlayToPlayerFromFile

Play an audio file to specific player. The audio path is recommended to be absolute path.

Params Type Description
slot int player slot
audioPath std::string or string audio path, absolute path is recommended
volume float default 1.0

Metamod Usage

pAudio->PlayToPlayerFromFile(slot, audioPath, volume);

CounterStrikeSharp Usage

Audio.PlayToPlayerFromFile(slot, audioPath, volume);

Swiftly Usage

audio:PlayToPlayerFromFile(slot, audioPath, volume)

PlayFromBuffer

Play an audio buffer to ALL players. The audio buffer can be in any format as long as it can be recognized by ffmpeg.

Params Type Description
audioBuffer std::string or string audio buffer
volume float default 1.0

Metamod Usage

pAudio->PlayFromBuffer(audioBuffer, volume);

CounterStrikeSharp Usage

Audio.PlayFromBuffer(audioBuffer, volume);

Swiftly Usage

audio:PlayFromBuffer(audioBuffer, volume)

PlayFromFile

Play an audio file to ALL players. The audio path is recommended to be absolute path.

Params Type Description
audioPath std::string or string audio path, absolute path is recommended
volume float default 1.0

Metamod Usage

pAudio->PlayFromFile(audioPath, volume);

CounterStrikeSharp Usage

Audio.PlayFromFile(audioPath, volume);

Swiftly Usage

audio:PlayFromFile(audioPath, volume)

IsPlaying

Return if theres an audio playing to specific player now.

Params Type Description
slot int player slot
Return Type Description
playing boolean playing or not

Metamod Usage

bool playing = pAudio->IsPlaying(slot);

CounterStrikeSharp Usage

bool playing = Audio.IsPlaying(slot);

Swiftly Usage

local playing = audio:IsPlaying(slot)

IsAllPlaying

Return if theres an audio playing to ALL players now.

Return Type Description
playing boolean playing or not

Metamod Usage

bool playing = pAudio->IsAllPlaying();

CounterStrikeSharp Usage

bool playing = Audio.IsAllPlaying();

Swiftly Usage

local playing = audio:IsAllPlaying()

RegisterPlayStartListener

Register a listener when a play starts.

Params Type Description
handler Function (int slot) => void the handler, slot is player slot, -1 if global
Return Type Description
id int The id of the registered handler. Use this to unregister it.

Metamod Usage

void Handler(int slot) 
{
  // xxx
}
pAudio->RegisterPlayStartListener(&Handler);

CounterStrikeSharp Usage

public static void PlayStartHandler(int slot)
{
  // xxx
}
Audio.RegisterPlayStartListener(PlayStartHandler);

Swiftly Usage

local handler = function (slot)
  // xxx
end
audio:RegisterPlayStartListener(handler)

UnregisterPlayStartListener

Unregister a listener when a play starts.

Params Type Description
handler or handler id int or Function the handler, metamod use handler id, css and swiftly use handler

Metamod Usage

int id = pAudio->RegisterPlayStartListener(&Handler);
pAudio->UnregisterPlayStartListener(id);

CounterStrikeSharp Usage

Audio.UnregisterPlayStartListener(PlayStartHandler);

Swiftly Usage

audio:UnregisterPlayStartListener(handler)

RegisterPlayEndListener

Register a listener when a play ends.

Params Type Description
handler Function (int slot) => void the handler, slot is player slot, -1 if global
Return Type Description
id int The id of the registered handler. Use this to unregister it.

Metamod Usage

void Handler(int slot) 
{
  // xxx
}
pAudio->RegisterPlayEndListener(&Handler);

CounterStrikeSharp Usage

public static void PlayEndHandler(int slot)
{
  // xxx
}
Audio.RegisterPlayEndListener(PlayEndHandler);

Swiftly Usage

local handler = function (slot)
  // xxx
end
audio:RegisterPlayEndListener(handler)

UnregisterPlayEndListener

Unregister a listener when a play ends.

Params Type Description
handler or handler id int or Function the handler, metamod use handler id, css and swiftly use handler

Metamod Usage

int id = pAudio->RegisterPlayEndListener(&Handler);
pAudio->UnregisterPlayEndListener(id);

CounterStrikeSharp Usage

Audio.UnregisterPlayEndListener(PlayEndHandler);

Swiftly Usage

audio:UnregisterPlayEndListener(handler)

RegisterPlayListener

Register a listener when a play net message being sent, basically will be called per 40 milliseconds.

Params Type Description
handler Function (int slot, int progress) => void the handler, slot is player slot, -1 if global, progress is in milliseconds
Return Type Description
id int The id of the registered handler. Use this to unregister it.

Metamod Usage

void Handler(int slot, int progress) 
{
  // xxx
}
pAudio->RegisterPlayListener(&Handler);

CounterStrikeSharp Usage

public static void PlayHandler(int slot, int progress)
{
  // xxx
}
Audio.RegisterPlayListener(PlayHandler);

Swiftly Usage

local handler = function (slot, progress)
  // xxx
end
audio:RegisterPlayListener(handler)

UnregisterPlayListener

Unregister the play listener.

Params Type Description
handler or handler id int or Function the handler, metamod use handler id, css and swiftly use handler

Metamod Usage

int id = pAudio->RegisterPlayListener(&Handler);
pAudio->UnregisterPlayListener(id);

CounterStrikeSharp Usage

Audio.UnregisterPlayListener(PlayHandler);

Swiftly Usage

audio:UnregisterPlayListener(handler)

SetPlayer

Set the player of the audio.

Params Type Description
slot int default -1, can be invalid, when it's valid, the game will show the player with that slot as the player of the audio, when it's invalid, the game won't show the player

Metamod Usage

pAudio->SetPlayer(slot);

CounterStrikeSharp Usage

Audio.SetPlayer(slot);

Swiftly Usage

audio:SetPlayer(slot)