Skip to content

TTMSFNCWXAudioPlayer

The TTMSFNCWXAudioPlayer is a component that plays audio from a URL. Browser-native controls can be displayed inside the component, or playback can be driven entirely from code using the exposed properties, methods, and events.

Make sure to consult the supported audio codecs before selecting a format for your audio files.

Loading audio

Set the URL property to point to an audio file. The value can be an HTTP/HTTPS URL or a local file using the file:// protocol.

procedure TForm1.FormCreate(Sender: TObject);
begin
  TMSFNCWXAudioPlayer1.URL := 'https://example.com/audio/sample.mp3';
end;

After the component initializes the OnAudioInitialized event fires. If AutoPlay is True the audio begins playing immediately after the source is loaded.

Controlling playback

procedure TForm1.Button1Click(Sender: TObject);
begin
  TMSFNCWXAudioPlayer1.Play;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  TMSFNCWXAudioPlayer1.Pause;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  // Skip forward 10 seconds
  TMSFNCWXAudioPlayer1.ForwardAudio(10);
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  // Rewind 10 seconds
  TMSFNCWXAudioPlayer1.RewindAudio(10);
end;

Tracking playback state

Use the read-only IsPlaying and Duration properties to inspect the current state at any time. The CurrentTime property returns the current playback position in seconds and can also be set to seek to a specific position.

procedure TForm1.Button5Click(Sender: TObject);
begin
  // Seek to the halfway point
  TMSFNCWXAudioPlayer1.CurrentTime := TMSFNCWXAudioPlayer1.Duration / 2;
end;

Preloading

The Preload property controls how eagerly the browser fetches audio data before playback starts.

Value Description
plNone The browser should not preload the audio.
plMetadata Only metadata (duration, tracks) is preloaded.
plAuto The browser may download the entire file in the background.

Playback rate and volume

PlayBackRate accepts a Double value where 1.0 is normal speed. Volume accepts a Double between 0.0 (silent) and 1.0 (full volume).

procedure TForm1.FormCreate(Sender: TObject);
begin
  TMSFNCWXAudioPlayer1.PlayBackRate := 1.5;  // 1.5x speed
  TMSFNCWXAudioPlayer1.Volume := 0.8;         // 80% volume
  TMSFNCWXAudioPlayer1.Loop := True;
  TMSFNCWXAudioPlayer1.Muted := False;
end;

Reacting to events

procedure TForm1.TMSFNCWXAudioPlayer1MetaDataLoaded(Sender: TObject);
begin
  Label1.Caption := 'Duration: ' + FloatToStr(TMSFNCWXAudioPlayer1.Duration) + 's';
end;

procedure TForm1.TMSFNCWXAudioPlayer1TimeUpdate(Sender: TObject);
begin
  ProgressBar1.Position := Round(TMSFNCWXAudioPlayer1.CurrentTime);
end;

procedure TForm1.TMSFNCWXAudioPlayer1Ended(Sender: TObject);
begin
  ShowMessage('Playback finished.');
end;

Properties

Property Type Default Description
AutoPlay Boolean False Automatically starts playback as soon as the source is loaded.
Controls Boolean True Shows or hides the built-in browser audio controls inside the component.
CurrentTime Double 0 Gets or sets the current playback position in seconds.
Duration Double Read-only. Total duration of the loaded audio in seconds.
EnableContextMenu Boolean False Enables the browser right-click context menu on the control.
EnableShowDebugConsole Boolean False Shows the browser developer tools console for debugging.
IsPlaying Boolean Read-only. True when audio is currently playing.
Loop Boolean False Restarts playback automatically when the audio ends.
Muted Boolean False Mutes the audio output.
PlayBackRate Double 1.0 Playback speed multiplier. 1.0 is normal speed.
Preload TTMSFNCWXMediaPreload plAuto Controls how the browser preloads the audio (plNone, plMetadata, plAuto).
URL string '' The audio source URL. Supports HTTP/HTTPS and the file:// protocol.
Volume Double 1.0 Playback volume. Range: 0.0 (silent) to 1.0 (maximum).

Methods

Method Description
ForwardAudio(ASeconds: Integer) Skips forward ASeconds seconds in the audio.
Pause Pauses playback at the current position.
Play Starts or resumes playback.
RewindAudio(ASeconds: Integer) Rewinds ASeconds seconds in the audio.

Events

Event Description
OnAudioInitialized Fires when the audio component has finished initializing.
OnEnded Fires when playback reaches the end of the audio.
OnError Fires when a playback error occurs.
OnMetaDataLoaded Fires when the audio metadata (e.g. duration) has been loaded.
OnPause Fires when playback is paused.
OnPlay Fires when playback starts.
OnRateChange Fires when PlayBackRate changes.
OnTimeUpdate Fires continuously as CurrentTime advances during playback.
OnVolumeChange Fires when Volume or Muted changes.