Guide
Media Events
STMediaRunner performs many operations asynchronously. Rather than requiring separate callback models for playback, recording, recognition, permissions, device changes, and audio interruptions, SpeechTiles exposes a common typed media-event model.
Observing events
Create and retain an STEventsObserver, assign its delegate, and implement STMediaEventDelegate.
final class MediaController: STMediaEventDelegate {
private let eventsObserver = STEventsObserver()
init() {
eventsObserver.delegate = self
}
func mediaEventReceived(_ event: STMediaEvent) {
switch (event.function, event.type) {
case (.speechRecognition, .srPartialHypothesis):
if case .speechRecognition(let data) = event.data {
print(data.hypothesis ?? "")
}
case (.audioRecording, .ended):
if case .recording(let data) = event.data {
print(data.stopReason as Any)
}
default:
break
}
}
}
Retain the observer for as long as events need to be received. Its delegate is weak.
Event model
Each event has four layers:
mediaJobIDandmediaItemIDcorrelate the event with application work.STMediaFunctionidentifies the media subsystem/activity.STMediaEventTypeidentifies what happened.STMediaEventDatacarries the typed payload, when the event needs one.
This design lets the same event channel represent playback lifecycle, recording/VAD, speech recognition, application and system state, audio devices/routes, interruptions, permissions, media availability, and barge-in without falling back to untyped payload dictionaries.