Scripting Tutorial: Custom Audio

From GTA Connected
Revision as of 10:32, 28 August 2022 by Vortrex (talk | contribs) (Created page with "{{DISPLAYTITLE:Scripting Tutorial: Custom Audio}} == What is it? == GTA Connected provides scripting functions to play custom audio sounds in-game. These are '''not''' replac...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search


What is it?

GTA Connected provides scripting functions to play custom audio sounds in-game. These are not replacements of in-game sounds, but instead just extra sounds and audio you can play whenever.

Getting started

Create the sound first. This can be done one of two ways: file or internet stream. See the audio file or internet stream sections below for information on how to set them up.

Using audio file

The audio file option is used with audio.createSound. You'll need to add the audio file to meta.xml so the client caches it. Example:

let audioObject = null;
let audioFile = openFile("audio-file.mp3");
if (audioFile) {
    audioObject = audio.createSound(audioFile);
    audioFile.close();
}

Using internet stream

The internet stream option is used with audio.createSoundFromURL. Example:

let audioObject = audio.createSoundFromURL("http://example.com/audio-file.mp3");

Next step

Using one of the two methods above, you now have a sound object you can use with sound.play, sound.stop, sound.position and sound.volume

Important notes

  • For the audio file option, you can use an OnResourceReady event handler to detect when all the client files are finished downloading. If you try to load the audio file before the client files are downloaded, it will fail.
  • The sound.position property is a seek position, not a game world position. It indicates where in the track the audio playback is currently
  • The sound.volume range is 0 (mute) to 1 (full volume). If you want to use whole number percentage (0-100, like 50 for half volume) you can divide the number by 100 to get the correct value.