playAudio
Starts asynchronous playback of an audio segment.
An audio segment may contain a recorded audio resource, direct text for speech synthesis, or text loaded from a resource URL.
Declaration
public func playAudio(
_ mediaJobID: String,
audioSegment: STAudioSegment,
audioPlayParams: STAudioPlayParams?,
isFinalSegment: Bool = true
) -> STResultOverview
Starts asynchronous playback of an audio segment. Check the documented prerequisites, parameters and return value below. For asynchronous operations, use the relevant media events to observe actual completion rather than treating an accepted request as finished.
Prerequisite
Initialize the audio player with initializeAudioPlayer() before calling this method.
Parameters
mediaJobID
An application-assigned identifier for the larger media job.
SpeechTiles includes this identifier with related media events so the application can correlate asynchronous events with the operation that initiated them.
audioSegment
The individual item to play.
STAudioSegment.mediaItemID provides a second level of identity for the segment itself.
audioPlayParams
Optional per-operation playback and speech-synthesis settings.
Pass nil when no per-operation playback parameters are required.
isFinalSegment
Indicates whether this segment is the final segment in the application's current playback collection.
Default: true.
This distinction is relevant when recording or speech recognition is active while multiple audio segments are being played, because the capture side can distinguish intermediate playback completion from completion of the final segment.
Return value
Returns .success when the playback request is accepted. Playback then proceeds asynchronously.
A non-success STResult indicates that the request could not be started, for example because the player is not initialized, the player is not in a valid state, permissions are unavailable, or the supplied segment cannot be played.
Events
Observe playback progress and completion through STEventsObserver and STMediaEventDelegate.
Related events carry the media job ID, media item ID, media function, event type, and typed event data.
Example — synthesized text
let segment = STAudioSegment(
source: .text("Welcome to SpeechTiles."),
mediaItemID: "welcome"
)
let result = STMediaRunner.shared.playAudio(
"onboarding",
audioSegment: segment,
audioPlayParams: nil
)
Example — one segment in a multi-segment job
let result = STMediaRunner.shared.playAudio(
"instructions",
audioSegment: firstSegment,
audioPlayParams: params,
isFinalSegment: false
)
A later segment in the same logical playback collection can use the same mediaJobID and set isFinalSegment: true.
Final segment coordination
isFinalSegment tells the media layer whether this segment is the last segment in a collection. This matters when audio recording or speech recognition is active during playback: the capture side needs to distinguish an intermediate segment ending from the end of the complete playback collection.
Default: true.