Audio Playlist

TGorillaAudioPlaylist is a non-visual component that plays a list of audio tracks — with shuffle, loop and automatic advance to the next track. It works headless, so it fits a background music system just as well as a media player UI.

Table of Contents

Overview

Class Description
TGorillaAudioPlaylist The component: track list, playback, shuffle, loop
TGorillaPlaylistCollection The owned collection of tracks
TPlaylistItem One track — Name and FilePath
TAudioDriver FMOD or OpenAL

The playlist does not implement audio playback itself; it drives a TGorillaFMODAudioManager or a TGorillaOpenALManager. Either link an existing manager from your form, or let the playlist create one internally on first playback.

Getting Started

uses Gorilla.Audio.Playlist;

var LPlaylist : TGorillaAudioPlaylist;
begin
  LPlaylist := TGorillaAudioPlaylist.Create(Self);
  LPlaylist.Driver := TAudioDriver.OpenAL;
  LPlaylist.Volume := 0.6;

  LPlaylist.AddToPlaylist('music/menu.ogg');
  LPlaylist.AddToPlaylist('music/battle.ogg');
  LPlaylist.AddToPlaylist('music/credits.ogg');

  LPlaylist.LoopMode     := true;
  LPlaylist.AutoPlayNext := true;
  LPlaylist.Play();
end;

Because Items is a published collection, the same list can be filled at design-time through the Object Inspector's collection editor.

Choosing the Driver

// let the playlist create and own a manager
LPlaylist.Driver := TAudioDriver.FMOD;

// or link a manager that already exists on the form
LPlaylist.Driver      := TAudioDriver.OpenAL;
LPlaylist.OpenALManager := GorillaOpenALManager1;

A manager linked through FMODManager / OpenALManager stays owned by the form; one created by the playlist is released with it. CreateAudioHandler() and DestroyAudioHandler() control that instance explicitly, and the read-only FMOD / OpenAL properties give access to whichever manager is currently in use.

Link an existing manager whenever the rest of your game already uses one — running two audio devices side by side wastes voices and complicates mixing.

Managing the Track List

LIndex := LPlaylist.AddToPlaylist('music/boss.ogg');
LPlaylist.RemoveFromPlaylist(2);
LPlaylist.MovePlaylistItem(3, 0);
LPlaylist.ClearPlaylist();

LItem := LPlaylist.GetPlaylistItem(0);
ShowMessage(LItem.Name + ' -> ' + LItem.FilePath);

Caption := Format('%d tracks, now playing %s',
  [LPlaylist.PlaylistCount, LPlaylist.CurrentTrackName]);

AddToPlaylist() derives the display name from the file name. Setting FilePath on an item updates that name as well, so a track list stays readable without extra work.

Audio can also be loaded directly, bypassing the list — useful for a one-off stinger:

LPlaylist.LoadAudio('music/intro.ogg');
LPlaylist.LoadAudio(LMemoryStream);

Playback Control

LPlaylist.Play();
LPlaylist.Pause();
LPlaylist.Stop();

LPlaylist.PlayItem(2);
LPlaylist.PlayNext();
LPlaylist.PlayPrevious();

if LPlaylist.IsPlaying then ...;
if LPlaylist.IsPaused  then ...;

LPos := LPlaylist.GetPlaybackPosition();   // ms
LLen := LPlaylist.GetPlaybackLength();     // ms
LPlaylist.SetPlaybackPosition(30000);      // seek to 0:30

Shuffle and Loop

Property Effect
ShuffleMode Plays the list in a random order; the order is regenerated whenever the list changes
LoopMode Starts over at the first track after the last one
AutoPlayNext Advances to the next track when one finishes

Shuffle works on a permutation, not on a random pick per track — every track is played once before any repeats.

Driving the Playlist Yourself

By default the component polls playback on an internal timer and advances on its own. When an external timer or your render loop already drives the audio, switch that off and call UpdatePlayback() instead:

LPlaylist.AutoUpdate := false;
// ...
procedure TForm1.RenderTimer(Sender : TObject);
var LFinished : Boolean;
    LPos      : UInt32;
begin
  LPos := LPlaylist.UpdatePlayback(LFinished);
  ProgressBar1.Value := LPos;
  if LFinished then
    LPlaylist.PlayNext();
end;

UpdatePlayback() returns the current position in milliseconds and reports through its out parameter whether the track has ended. The component latches the actual start of playback, so a track is never reported as finished during the start-up window.

Events

Event Fired when
OnPlaylistChanged Tracks were added, removed, moved or edited
OnTrackChanged A different track became current
OnPlaybackFinished The current track ended
OnStateChanged Playing / paused / stopped changed

Property Reference

Property Default Description
Items — The track collection, editable at design-time
Driver FMOD Audio backend to use
FMODManager — Existing FMOD manager to drive
OpenALManager — Existing OpenAL manager to drive
AutoUpdate True Polls playback and advances on its own
AutoPlayNext True Continues with the next track automatically
ShuffleMode False Random play order
LoopMode False Restart after the last track
Volume — Playback volume, 0.0 … 1.0

Read-only: Playlist, PlaylistCount, CurrentIndex, CurrentTrackName, FMOD, OpenAL.

Best Practices

  • Link an existing audio manager instead of letting the playlist create a second one
  • Keep AutoUpdate on unless something else already drives the audio each frame
  • Use OnTrackChanged to update a "now playing" label instead of polling
  • For seamless background music, combine LoopMode with AutoPlayNext
  • Prefer streamed formats (OGG) for music — a full decode of a long track costs a lot of memory

See Also

API Reference

Related Topics