Jamie Balfour

Welcome to my personal website.

Find out more about me, my personal projects, reviews, courses and much more here.

Official ZPE/YASS documentationAudioStream

Introduction

The AudioStream object provides a simple way to load and play short audio clips from a URL or file path. Internally it uses Java's javax.sound.sampled.Clip implementation.

The typical workflow is:

  • Create a new AudioStream instance
  • Open an audio stream using open_stream
  • Play the audio either synchronously (play) or asynchronously (play_async)
  • Optionally query the clip length using length

All functions exposed by this object require a permission level of 0.

AudioStream object functions

The following is a list of internal functions the AudioStream object exposes. All functions are ZPEObjectNativeFunctions therefore run in native code.

open_stream(string url) ⇒ boolean
Opens an audio stream from a URL or file path and prepares it for playback. Returns true if the stream was successfully opened, otherwise false.
play() ⇒ boolean
Plays the currently opened clip and blocks until playback has finished. Once finished, the internal frame position is reset to the start of the clip. Returns true.
play_async() ⇒ boolean
Plays the currently opened clip without blocking the current script. The frame position is reset to the start immediately after starting playback. Returns true.
length() ⇒ number
Returns the length of the currently opened clip in milliseconds.

Examples

This example opens a clip and plays it synchronously (the program will pause until the clip finishes):

YASS
$audio = new AudioStream()

if ($audio->open_stream("sounds/notify.wav"))
    print("Length (ms): " + $audio->length())
    $audio->play()
else
    print("Failed to open audio stream.")
end

This example plays audio asynchronously, allowing your program to continue running:

YASS
$audio = new AudioStream()

if ($audio->open_stream("sounds/alert.wav"))
    $audio->play_async()
    print("Playing... program continues immediately.")
end

# Do other work here...

Notes:

  • Stream must be opened first: You must call open_stream before calling play, play_async or length.
  • Synchronous vs asynchronous: play blocks the script until the clip finishes, while play_async returns immediately.
  • Clip length: length returns the duration in milliseconds.
  • Reset behaviour: Both play and play_async reset the clip back to the start so it can be played again without re-opening.
Comments

There are no comments on this page.

New comment

Comments are welcome and encouraged, including disagreement and critique. However, this is not a space for abuse. Disagreement is welcome; personal attacks, harassment, or hate will be removed instantly. This site reflects personal opinions, not universal truths. If you can’t distinguish between the two, this probably isn’t the place for you. The system temporarily stores IP addresses and browser user agents for the purposes of spam prevention, moderation, and safeguarding. This data is automatically removed after fourteen days. Your email address is stored so that replies can be sent to your email address.

Comments powered by BalfComment

Feedback 👍
Comments are sent via email to me.