I need help with fixing this issue that I am facing. The app is supposed to queue audio files, so when audio is playing from a checkpoint, then the next checkpoint is reached and triggered, the expected behavior is that it will not immediately initiate any change. This is not what is happening though, currently the closed captions are being triggered to the next checkpoint’s closed captions. This causes different closed captions to be playing from the audio that is playing. Here is part of the "TourSystem" class in the file "System.swift":
func playAudioForCheckpoint(_ checkpoint: Checkpoint) {
code
let audioSet = checkpoint.audio
print("now playing \(audioSet.foreground)")
var result: PlayFileStatus?
//TODO see if we can make this queued audio logic work. it works incoonsistently. see life track checkpoints 1-3 for example of inconsistent queing behaviour
if alwaysQueueAudio {
result = AudioSystem.shared.queueFileInForeground(fileName: audioSet.foreground)
} else if let queueAudio: Bool = checkpoint.queueAudio,
queueAudio == true
{
result = AudioSystem.shared.queueFileInForeground(fileName: audioSet.foreground)
} else {
AudioSystem.shared.foregroundAudioNode.stop()
AudioSystem.shared.foregroundAudioNode.reset()
result = AudioSystem.shared.playFileInForeground(
fileName: audioSet.foreground, captionFile: audioSet.closedCaption)
}
if result != nil {
// handlePlayResult(result: result!)
// foregroundAudioLabel.text = "Fore Audio: Error"
state.currentForegroundAudio = "Error"
print("failed to play foregrouned audio due to error \(result)")
} else {
// foregroundAudioLabel.text = "Fore Audio: \(audioSet.foreground)"
// tour.state.currentForegroundAudio = audioSet.foreground
state.currentForegroundAudio = audioSet.foreground
print("playing foreground audio file \(audioSet.foreground)")
}
let currentBackgroundAudioFile: AudioFileID = AudioSystem.shared.backgroundAudioFile
if let backgroundAudioFile: AudioFileID = audioSet.background,
backgroundAudioFile != currentBackgroundAudioFile
{
AudioSystem.shared.backgroundAudioNode.stop()
result = AudioSystem.shared.playFileInBackground(fileName: backgroundAudioFile)
if result != nil {
// handlePlayResult(result: result!)
// backgroundAudioLabel.text = "Back Audio: Error"
// tour.state.currentBackgroundAudio = "error"
state.currentBackgroundAudio = ""
print(
"Could not playing background audio file \(backgroundAudioFile) due to error \(result)")
} else {
// backgroundAudioLabel.text = "Back Audio: \(backgroundAudioFile)"
// tour.state.currentBackgroundAudio = backgroundAudioFile
state.currentBackgroundAudio = backgroundAudioFile
print("playing background audio file \(backgroundAudioFile)")
}
} else {
print(
"not setting new background audio because checkpoint does not specify any background audio")
}
Assistant
}
func initializeSRTParser() -> SRTParser? {
let audioFileName = AudioSystem.shared.foregroundAudioFile
code
let srtFileName = (audioFileName as NSString).deletingPathExtension
print("Attempting to load SRT file: \(audioFileName).srt")
print("Attempting to load SRT file: \(srtFileName).srt")
let srtURL =
Bundle.main.url(forResource: srtFileName, withExtension: "srt")
?? Bundle(for: type(of: self)).url(forResource: srtFileName, withExtension: "srt")
if let url = srtURL {
print("SRT file found at: \(url.path)")
return SRTParser(fileURL: url)
} else {
print("SRT file not found for: \(srtFileName).srt")
return nil
}
}
private var captionTimerID: UUID?
private var captionStartTime: Date?
var isCaptionRunning: Bool = false
var currentCaptionFile: String?
func startClosedCaptions(time: Double, file: String) {
print("DEBUG: startClosedCaptions called with file: (file)")
code
// Check if captions are already running for this file
if isCaptionRunning && currentCaptionFile == file {
print("DEBUG: Captions already running for \(file)")
return
}
stopClosedCaptions()
clearClosedCaptions()
currentCaptionFile = file
isCaptionRunning = true
let srtFileName = (file as NSString).deletingPathExtension
currentSRTParser = initializeSRTParser()
guard let parser = currentSRTParser else {
print("DEBUG: Parser not initialized.")
return
}
guard AudioSystem.shared.foregroundAudioNode.isStarted else {
print("Audio not started, cannot start captions.")
return
}
let maxDuration = AudioSystem.shared.foregroundAudioDuration
let timerID = UUID()
captionTimerID = timerID
print("DEBUG: Starting caption timer with ID: \(timerID)")
captionStartTime = Date().addingTimeInterval(-time)
captionTimer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { [weak self] timer in
guard let self = self, self.isCaptionRunning, let startTime = self.captionStartTime else {
timer.invalidate()
return
}
let currentTime = Date().timeIntervalSince(startTime)
if AudioSystem.shared.foregroundAudioNode.isStarted && currentTime < maxDuration {
if let caption = parser.getCaption(for: currentTime) {
DispatchQueue.main.async {
self.gameViewController?.closedCaptionsLabel.text = caption
}
}
} else {
print(
"Stopping captions. Audio playing: \(AudioSystem.shared.foregroundAudioNode.isStarted)")
self.stopClosedCaptions()
}
}
func transit(_ transition: Transition) {
switch transition {
case .nextCheckpoint:
nextCheckpoint()
case .startPath(let path):
state.currentPath = path
state.currentCheckpointIndex = 0
state.timeArrivedAtLastCheckpoint = Date()
print("we've started a new path: (path.name)")
case .endTour:
print("we're ending the tour")
case .contingency:
print("were handling a contingency (?)")
}
}
var result = AudioSystem.shared.queueFileInForeground(fileName: audioFile) //TODORELEASE make sure queueing is right beahviour for timeout audio
if result != nil {
// handlePlayResult(result: result!)
// foregroundAudioLabel.text = "Fore Audio: Error"
switch result {
case .noSuchFile(let filename):
state.currentForegroundAudio = "Error: File does not exist. Try setting it."
default: state.currentForegroundAudio = "Error"
}
print("failed to play foregrouned audio due to error \(result)")
} else {
// foregroundAudioLabel.text = "Fore Audio: \(audioSet.foreground)"
// tour.state.currentForegroundAudio = audioSet.foreground
state.currentForegroundAudio = audioFile
print("playing foreground audio file \(audioFile)")
}
}
private var timeoutContingencyTimer: Timer?
private func setupTimeoutContingency(timeout: TimeInterval, audioFile: AudioFileID) {
print(
"settingup timeout contingency for checkpoint to activate in (timeout) and play (audioFile)"
)
code
let timeoutTimerSetupDelay: TimeInterval = 1
// let foreAud = AudioSystem.shared.foregroundAudioNode
// foreAud.buffer
Timer.scheduledTimer(withTimeInterval: timeoutTimerSetupDelay, repeats: false) { _ in
let remainingForegroundAudioDuration =
AudioSystem.shared.foregroundAudioDuration
- AudioSystem.shared.foregroundAudioNode.getCurrentTime()
print(
"setting up timeout contingency. specified timeout time is \(timeout) and the current foreground audio has duration \(AudioSystem.shared.foregroundAudioDuration) and current playtime \(AudioSystem.shared.foregroundAudioNode.getCurrentTime()) and so still has \(remainingForegroundAudioDuration)s left, so we'll play the timeout contingency audio file \"\(audioFile)\" in \(timeout + remainingForegroundAudioDuration - timeoutTimerSetupDelay)s"
)
// Check if we need to reset the time, and do it only once
//TODORELEASE NATE Check prior with beacons, might be unnecessary to do
// Reinitialize the parser each time
TourSystem.srtParser = self.initializeSRTParser()
guard let parser = TourSystem.srtParser else {
print("Parser not initialized.")
return
}
let isIntro003 = AudioSystem.shared.foregroundAudioFile == "003 Intro.mp3"
var captionStartTime: TimeInterval = 4
var hasStartedCaptions = false
self.timeoutContingencyTimer = Timer.scheduledTimer(
withTimeInterval: 0.1, // Check every 0.1 seconds
repeats: true,
block: { [weak self] timer in
guard let self = self else {
timer.invalidate()
return
}
let currentTime = AudioSystem.shared.foregroundAudioNode.getCurrentTime()
// if isIntro003 {
// if !hasStartedCaptions && currentTime > 0 {
// captionStartTime = currentTime
// hasStartedCaptions = true
// print("Starting captions for 003 Intro.mp3 at time: \(captionStartTime)")
// }
// var maxDuration = AudioSystem.durationOfAudio(audioFileName: "003 Intro.mp3")
// var isPlaying = AudioSystem.shared.foregroundAudioNode.isStarted
// if isPlaying && hasStartedCaptions && currentTime < maxDuration! {
// let adjustedTime = currentTime - captionStartTime
// if let caption = parser.getCaption(for: adjustedTime) {
// print("Caption found for adjusted time \(adjustedTime): \(caption)")
// DispatchQueue.main.async {
// self.gameViewController?.closedCaptionsLabel.text = caption
// }
// }
// }
// }
// Check if it's time to play the contingency audio
if currentTime >= timeout + remainingForegroundAudioDuration - timeoutTimerSetupDelay {
self.playTimeoutAudio(audioFile)
timer.invalidate()
}
})
self.timeoutContingencyTimer = Timer.scheduledTimer(
withTimeInterval: timeout + remainingForegroundAudioDuration - timeoutTimerSetupDelay,
repeats: false,
block: { timer in
self.playTimeoutAudio(audioFile)
timer.invalidate()
})
}
// VALocation.shared.react(to: .didExitRegion(3)) { change in
// print("we've gone off track, according to \(change), so we're printing this")
// }
// VALocation.shared.event(.didExitRegion(3))
Localizer.shared.on(localization: localization) {
AudioSystem.shared.queueFileInForeground(fileName: audioFile) //TODO handle result of this call, handle possible errors //TODORELEASE make sure queing behaviour is appropriate
Localizer.shared.stopReacting(to: localization.id) //TODO determine if it makes sense to remove this reaction or simply to pause if for a while and then reenable it for later?
}
}
//TODO NATE look at this and decide if it needs to be extended or shortend.
private let delayForOfftrackContigencies: TimeInterval = 10 //TODOrelease make sure this is an appropriate value
private func handleContingencies(_ checkpoint: Checkpoint) {
if let (timeout, timeoutAudio): (TimeInterval, AudioFileID) = Util.getTimeoutContingency(
checkpoint: checkpoint)
{
setupTimeoutContingency(timeout: timeout, audioFile: timeoutAudio)
}
code
Timer.scheduledTimer(withTimeInterval: delayForOfftrackContigencies, repeats: false) { _ in
if let offtrackConts: [(Localization, AudioFileID)] = Util.getOfftrackContingency(
checkpoint: checkpoint)
{
for (localization, offTrackAudio) in offtrackConts {
self.setupOfftrackContingency(
localization: localization, audioFile: offTrackAudio)
}
}
}
}
func setTriggerForNextCheckpoint() {
print(
"DEBUG: setTriggerForNextCheckpoint called for checkpoint index: (state.currentCheckpointIndex)"
)
guard state.currentPath.checkpoints.count - 1 > state.currentCheckpointIndex else {
print(
"already at last checkpoint in the path, so setting up trigger for end of path conditions")
//this can be special path specific handling code, hard coded, because we only have 4 possibilities
// print("error: not implemented")
code
//TODO handle end of path special cases here
//Case: end of start path
let currentCheckpoint = state.currentPath.checkpoints[state.currentCheckpointIndex]
let currentPath = state.currentPath
switch currentPath.name {
case "Start Path":
let deathTrackStartLocalization = AmbroseLocations.southSideAlleyway
let lifeTrackStartLocalization = AmbroseLocations.lobbyStairsBottom
print(
"we've reached the last checkpoint of the start path; setting up special triggers for life and death paths"
)
var dstart = false
Localizer.shared.on(localization: deathTrackStartLocalization) {
guard !dstart else { return }
dstart = true
print(
"we've reached the trigger location for the start of the death path, setting up death path logic"
)
Localizer.shared.stopReacting(to: deathTrackStartLocalization.id)
Localizer.shared.stopReacting(to: lifeTrackStartLocalization.id)
self.state.currentPath = AmbroseTour.deathPath
self.state.currentCheckpointIndex = -1
self.nextCheckpoint()
//TODO update completedCheckpoints list
}
var lstart = false
Localizer.shared.on(localization: lifeTrackStartLocalization) {
guard !lstart else { return }
lstart = true
print(
"we've reached the trigger location for the start of the life path, setting up life path logic"
)
Localizer.shared.stopReacting(to: deathTrackStartLocalization.id)
Localizer.shared.stopReacting(to: lifeTrackStartLocalization.id)
self.state.currentPath = AmbroseTour.lifePath
self.state.currentCheckpointIndex = -1
self.nextCheckpoint()
//TODO update completedCheckpoints list
}
case "Death Path":
print(
"we've reached the last checkpoint of the death path; setting up special triggers for next paths"
)
//TODO setup handling of offtrack contingencies for end of death track, which aren't handled normally, because this is the last node of the path
let endPathStartLocalization = AmbroseLocations.levelOneCarPark
var estart = false
Localizer.shared.on(localization: endPathStartLocalization) {
guard !estart else { return }
estart = true
print(
"we've reached the trigger location for start of the end path(from death path); setting up end path logic "
)
Localizer.shared.stopReacting(to: endPathStartLocalization.id)
self.state.currentPath = AmbroseTour.endPath
self.state.currentCheckpointIndex = -1
self.nextCheckpoint()
}
case "Life Path":
print(
"we've reached the last checkpoint of the life path; setting up special triggers for next paths"
)
let endPathStartLocalization = AmbroseLocations.levelOneCarPark
var estart = false
Localizer.shared.on(localization: endPathStartLocalization) {
guard !estart else { return }
estart = true
print(
"we've reached teh trigger location for start of the end path(from life path); setting up end path logic "
)
Localizer.shared.stopReacting(to: endPathStartLocalization.id)
self.state.currentPath = AmbroseTour.endPath
self.state.currentCheckpointIndex = -1
self.nextCheckpoint()
}
case "End Path":
print(
"we've reached the last checkpoint of the end path; setting up special triggers for next paths"
)
endOfTourLogic?()
default:
print(
"unhandled case for setting up end of path triggers for path with name \(currentPath.name) "
)
}
return
}
let nextCheckpointIndex = state.currentCheckpointIndex + 1
let nextCheckpoint = state.currentPath.checkpoints[nextCheckpointIndex]
guard let triggerLocalization: Localization = nextCheckpoint.trigger else {
print(
"error: next checkpoint after \(state.currentPath.checkpoints[state.currentCheckpointIndex].name) does not have a trigger beacon defined, cannot set up trigger without trigger beacon"
)
return
}
var processedNextCheckpoint = false
//get localization for localizationID
// if let localization:Localization = AmbroseLocations.localization[triggerBeaconID] {
Localizer.shared.on(localization: triggerLocalization) {
print(
"triggered (this basic print statement) reaction to beacon with name \(triggerLocalization.name) and with id \(triggerLocalization.id)"
)
guard !processedNextCheckpoint else { return }
processedNextCheckpoint = true
self.nextCheckpoint()
Localizer.shared.stopReacting(to: triggerLocalization.id) //TODO consider having previous triggers deleted by nextCheckpoint method
}
}
func nextCheckpoint() {
print("DEBUG: nextCheckpoint called for checkpoint index: (state.currentCheckpointIndex)")
stopClosedCaptions()
clearClosedCaptions()
//TODOlogic determine if it makes sense to invalidate the timeout contingency timer here or if its better to do it elsewhere
timeoutContingencyTimer?.invalidate()
timeoutContingencyTimer = nil
//TODO invalidate offtrack contingency handler/"spacer", once that is implemented
code
let indexOfCurrentCheckpoint: Int = state.currentCheckpointIndex
//TODOlogic determine if it makes sense to add this here or better elsewhere
if indexOfCurrentCheckpoint >= 0 {
state.completedCheckpoints.append(
state.currentPath.checkpoints[indexOfCurrentCheckpoint])
}
guard indexOfCurrentCheckpoint >= -1 else {
print(
"cannot proceed to next checkpoint, current checkpoing has invalid index \(indexOfCurrentCheckpoint), unable to determine next valid index"
)
return
}
guard indexOfCurrentCheckpoint < state.currentPath.checkpoints.count - 1 else {
print(
"cannot proceed to next checkpoint in path, checkpoint \"\(state.currentPath.checkpoints[state.currentCheckpointIndex].name)\" is the last checkpoint in the path"
)
return
}
let nextCheckpointIndex = indexOfCurrentCheckpoint + 1
let nextCheckpoint = state.currentPath.checkpoints[nextCheckpointIndex]
AudioSystem.shared.playOrQueueFileInForeground(
fileName: nextCheckpoint.audio.foreground,
captionFile: nextCheckpoint.audio.closedCaption
) { [weak self] in
if let closedCaptionFile = nextCheckpoint.audio.closedCaption {
self?.gameViewController?.initializeClosedCaptions(for: closedCaptionFile)
}
}
playAudioForCheckpoint(nextCheckpoint)
stopClosedCaptions()
clearClosedCaptions()
// TODO Nate check this for validity
if let closedCaptionFile = nextCheckpoint.audio.closedCaption {
print("DEBUG: Initializing closed captions for file: \(closedCaptionFile)")
gameViewController?.initializeClosedCaptions(for: closedCaptionFile)
// startClosedCaptions(time: 0.0, file: closedCaptionFile)
}
//IMPORTANT handleContingencies must be called AFTER playAUdioForCheckpoint because it relies on calculations of the currently playing foreground audio's length as determined by the current state of the foreground audio player
handleContingencies(nextCheckpoint)
state.currentCheckpointIndex = nextCheckpointIndex
state.timeArrivedAtLastCheckpoint = Date()
print(
"moved to next checkpoint \(state.currentPath.checkpoints[state.currentCheckpointIndex].name) in path \(state.currentPath.name) "
)
//TODO otherwise, setup handling of when the criteria for activation of the next checkpoint occurs
//TODO clear previous triggers ie clear the localizer of all previously defined reactions
// print("how many previous rangingin events before clearing: \(Localizer.shared.recentDetections.count)")
Localizer.shared.clearEventsOutsideWindow(before: Date())
// print("how many previous rangingin events after clearing: \(Localizer.shared.recentDetections.count)")
Localizer.shared.clearReactionMap() //TODOrelease make sure this is safe
setTriggerForNextCheckpoint()
//TODO unsetup contingencies from the now passed checkpoint, ie those contingencies from the checkpoint we've left should cease.
//WIP process new nodes contingencies, if any, setting up related subsystems, like timing subsystem or location subsystem, to notify the runner when associated contingency criteria have been met. setup callbacks to handle contingencies when they occur.
//TODO If the new current checkpoint is the last checkpoint in the path, instead setup handling of path end contingencies.
}
}
Here is the "AudioSystem" class in the "Audio.swift" file:
import AVFoundation
import AudioKit
import Foundation
enum PlayFileStatus {
case success
case noSuchFile(String)
case error(String)
}
class AudioSystem {
static var shared: AudioSystem = AudioSystem()
private(set) var currentAudioFile: String?
private(set) var currentCaptionFile: String?
private init() {
setupAudioGraph()
}
//MARK: audiokit setup
let engine = AudioEngine()
let backgroundAudioNode: AudioPlayer = AudioPlayer()
let foregroundAudioNode = AudioPlayer()
private var _backgroundAudioFile: String = ""
private var _foregroundAudioFile: String = ""
public var backgroundAudioFile: String { _backgroundAudioFile }
public var foregroundAudioFile: String { _foregroundAudioFile }
public var backgroundVolume: Float {
get { backgroundAudioNode.volume }
set {
backgroundAudioNode.volume = newValue
UserDefaults.standard.set(newValue, forKey: Preferences.backgroundVolumeKey)
}
}
private func setupAudioGraph() {
// let foregroundAudioNode = AudioPlayer()
// backgroundAudioNode = AudioPlayer()
let mainMixerNode = Mixer([foregroundAudioNode, backgroundAudioNode])
// guard !alwaysQueueAudio else { return queueFileInForeground(fileName: fileName)}
TourSystem.shared.stopClosedCaptions()
let docDir = try! FileManager.default.url(
for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let audioFileURL = docDir.appendingPathComponent(fileName, conformingTo: .audio)
do {
let audioFile = try AVAudioFile(forReading: audioFileURL) //todorelease don't force unwrap
let audioFileLength = AVAudioFrameCount(audioFile.length)
// print("SRT File: \(audioFileURL.lastPathComponent)")
let sampleRate = audioFile.fileFormat.sampleRate
let audioFileDuration: TimeInterval = TimeInterval(Double(audioFileLength) / sampleRate)
print(
"played audio file has duration \(audioFileDuration); setting this to the duration of the foregraound audio"
)
foregroundAudioDuration = audioFileDuration
foregroundAudioNode.stop()
foregroundAudioNode.reset()
// try foregroundAudioNode.load(file: audioFile)
foregroundAudioNode.playerNode.scheduleFile(
audioFile, at: AVAudioTime.now(), completionHandler: nil)
foregroundAudioNode.start()
// TourSystem.shared.startClosedCaptions(time: 0.0, file: fileName)
_foregroundAudioFile = fileName
currentAudioFile = fileName
currentCaptionFile = captionFile
foregroundAudioNode.start()
return nil
return nil
} catch {
print("failed to load audio file, it doesn' exist maybe, here's the error: \(error)")
_foregroundAudioFile = ""
return .noSuchFile(fileName)
}
}
static func durationOfAudio(audioFileName: String) -> TimeInterval? {
let docDir = try! FileManager.default.url(
for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let audioFileURL = docDir.appendingPathComponent(audioFileName, conformingTo: .audio)
do {
let audioFile = try AVAudioFile(forReading: audioFileURL) //todorelease don't force unwrap
// TODO NATE - fix durations so they access the duration property instead of the length/samplerate calc
// let fileDuration: TimeInterval = audioFile.duration
// print("audio file duration: (fileDuration)")
let audioFileLength = AVAudioFrameCount(audioFile.length)
let sampleRate = audioFile.fileFormat.sampleRate
let audioFileDuration: TimeInterval = TimeInterval(Double(audioFileLength) / sampleRate)
return audioFileDuration
} catch {
print("could not get audio file duration because of error (error)")
return nil
}
}
// func onCurrentAudioFinished() {
// let gameViewController: GameViewController = GameViewController()
// let currentCheckpoint = TourSystem.shared.state.currentPath.checkpoints[
// TourSystem.shared.state.currentCheckpointIndex]
// TourSystem.shared.playAudioForCheckpoint(currentCheckpoint)
// if let closedCaptionFile = currentCheckpoint.closedCaptionFile {
// print("DEBUG: Initializing closed captions for file: (closedCaptionFile)")
// gameViewController.initializeClosedCaptions(for: closedCaptionFile)
// }
// }
func queueFileInForeground(fileName: String) -> PlayFileStatus? {
let docDir = try! FileManager.default.url(
for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let audioFileURL = docDir.appendingPathComponent(fileName, conformingTo: .audio)
do {
let audioFile = try AVAudioFile(forReading: audioFileURL) //todorelease don't force unwrap
let audioFileLength = AVAudioFrameCount(audioFile.length)
let sampleRate = audioFile.fileFormat.sampleRate
let audioFileDuration: TimeInterval = TimeInterval(Double(audioFileLength) / sampleRate)
print(
"queued audio file has duration (audioFileDuration); adding this to the duration of the foregraound audio, which was (foregroundAudioDuration)"
)
To resolve the issue where closed captions are being prematurely triggered for the next checkpoint's audio, you need to ensure that captions are synchronized with the actual audio playback. Currently, when a checkpoint is triggered, the captions for the next checkpoint are initialized immediately, regardless of whether the audio has started playing or is queued to play later. This leads to a mismatch between the audio and the captions.
Solution Overview
Decouple Audio Playback and Caption Initialization:
Ensure that captions are only initialized when their corresponding audio starts playing.
Implement Callbacks for Audio Playback Start:
Modify the AudioSystem to notify the TourSystem when a queued audio file begins playback.
Use closures to trigger caption initialization at the right time.
Adjust TourSystem to Handle Callbacks Appropriately:
Initialize captions immediately if the audio is played right away.
Defer caption initialization if the audio is queued.
Step-by-Step Implementation
1. Define a Delegate or Use Closures for Notifications
To synchronize captions with audio playback, you can use closures (callbacks) that execute when an audio file starts playing.
Modify the AudioSystem Class:
Add a closure parameter to the queueFileInForeground and playOrQueueFileInForeground methods to notify when the audio starts.
swift
// AudioSystem.swift
class AudioSystem {
static var shared: AudioSystem = AudioSystem()
// Existing properties and initializations...
// Modify queueFileInForeground to accept a callback
func queueFileInForeground(fileName: String, captionStartClosure: (() -> Void)? = nil) -> PlayFileStatus? {
let docDir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let audioFileURL = docDir.appendingPathComponent(fileName, conformingTo: .audio)
do {
let audioFile = try AVAudioFile(forReading: audioFileURL)
let audioFileDuration = TimeInterval(Double(audioFile.length) / audioFile.fileFormat.sampleRate)
let currentTrackPlaybackTime = foregroundAudioNode.getCurrentTime()
let delay = foregroundAudioDuration - currentTrackPlaybackTime + 0.1
// Schedule the file
foregroundAudioNode.playerNode.scheduleFile(audioFile, at: AVAudioTime.now().offset(seconds: delay), completionHandler: nil)
// Schedule the caption initialization
if let closure = captionStartClosure {
Timer.scheduledTimer(withTimeInterval: delay, repeats: false) { _ in
closure()
}
}
_foregroundAudioFile = fileName
foregroundAudioDuration += audioFileDuration
foregroundAudioNode.play()
return nil
} catch {
print("Failed to load audio file: \(error)")
_foregroundAudioFile = ""
return .noSuchFile(fileName)
}
}
// Similarly, adjust playOrQueueFileInForeground if needed
func playOrQueueFileInForeground(fileName: String, captionFile: String?, captionStartClosure: (() -> Void)? = nil, completion: (() -> Void)? = nil) -> PlayFileStatus? {
let docDir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let audioFileURL = docDir.appendingPathComponent(fileName, conformingTo: .audio)
do {
let audioFile = try AVAudioFile(forReading: audioFileURL)
let audioFileDuration = TimeInterval(Double(audioFile.length) / audioFile.fileFormat.sampleRate)
let currentTrackPlaybackTime = foregroundAudioNode.getCurrentTime()
if currentTrackPlaybackTime < foregroundAudioDuration {
// Queue the file
foregroundAudioNode.playerNode.scheduleFile(audioFile, at: AVAudioTime.now().offset(seconds: foregroundAudioDuration - currentTrackPlaybackTime + 0.1)) {
DispatchQueue.main.async {
completion?()
}
}
// Schedule the caption initialization
if let closure = captionStartClosure {
Timer.scheduledTimer(withTimeInterval: foregroundAudioDuration - currentTrackPlaybackTime + 0.1, repeats: false) { _ in
closure()
}
}
foregroundAudioDuration += audioFileDuration
} else {
// Play the file immediately
foregroundAudioNode.stop()
foregroundAudioNode.reset()
foregroundAudioNode.playerNode.scheduleFile(audioFile, at: nil) {
DispatchQueue.main.async {
completion?()
}
}
foregroundAudioNode.start()
foregroundAudioDuration = audioFileDuration
// Initialize captions immediately
if let closure = captionStartClosure {
closure()
}
}
_foregroundAudioFile = fileName
currentAudioFile = fileName
currentCaptionFile = captionFile
foregroundAudioNode.play()
return nil
} catch {
print("Failed to load audio file: \(error)")
_foregroundAudioFile = ""
return .noSuchFile(fileName)
}
}
}
2. Adjust the TourSystem Class to Use Callbacks
Update the playAudioForCheckpoint and nextCheckpoint methods to handle caption initialization based on whether the audio is played immediately or queued.
Modify playAudioForCheckpoint:
swift
// System.swift
func playAudioForCheckpoint(_ checkpoint: Checkpoint) {
let audioSet = checkpoint.audio
print("now playing \(audioSet.foreground)")
var result: PlayFileStatus?
if alwaysQueueAudio {
result = AudioSystem.shared.queueFileInForeground(fileName: audioSet.foreground) {
// Initialize captions when the queued audio starts
self.startClosedCaptions(time: 0.0, file: audioSet.closedCaption ?? "")
}
} else if let queueAudio: Bool = checkpoint.queueAudio, queueAudio == true {
result = AudioSystem.shared.queueFileInForeground(fileName: audioSet.foreground) {
// Initialize captions when the queued audio starts
self.startClosedCaptions(time: 0.0, file: audioSet.closedCaption ?? "")
}
} else {
AudioSystem.shared.foregroundAudioNode.stop()
AudioSystem.shared.foregroundAudioNode.reset()
result = AudioSystem.shared.playFileInForeground(fileName: audioSet.foreground, captionFile: audioSet.closedCaption) {
// Initialize captions immediately since audio is played right away
self.startClosedCaptions(time: 0.0, file: audioSet.closedCaption ?? "")
}
}
if result != nil {
state.currentForegroundAudio = "Error"
print("failed to play foreground audio due to error \(String(describing: result))")
} else {
state.currentForegroundAudio = audioSet.foreground
print("playing foreground audio file \(audioSet.foreground)")
}
// Handle background audio as before...
}
Modify nextCheckpoint:
Ensure that captions are only initialized if the audio is played immediately. If the audio is queued, captions will be initialized via the callback.
swift
func nextCheckpoint() {
print("DEBUG: nextCheckpoint called for checkpoint index: \(state.currentCheckpointIndex)")
stopClosedCaptions()
clearClosedCaptions()
timeoutContingencyTimer?.invalidate()
timeoutContingencyTimer = nil
let indexOfCurrentCheckpoint = state.currentCheckpointIndex
if indexOfCurrentCheckpoint >= 0 {
state.completedCheckpoints.append(state.currentPath.checkpoints[indexOfCurrentCheckpoint])
}
guard indexOfCurrentCheckpoint >= -1 else {
print("Invalid checkpoint index.")
return
}
guard indexOfCurrentCheckpoint < state.currentPath.checkpoints.count - 1 else {
print("Last checkpoint in the path reached.")
// Handle end of path logic...
return
}
let nextCheckpointIndex = indexOfCurrentCheckpoint + 1
let nextCheckpoint = state.currentPath.checkpoints[nextCheckpointIndex]
// Play or queue the audio with a caption callback
AudioSystem.shared.playOrQueueFileInForeground(
fileName: nextCheckpoint.audio.foreground,
captionFile: nextCheckpoint.audio.closedCaption
) {
if let closedCaptionFile = nextCheckpoint.audio.closedCaption {
self.startClosedCaptions(time: 0.0, file: closedCaptionFile)
}
}
playAudioForCheckpoint(nextCheckpoint)
stopClosedCaptions()
clearClosedCaptions()
// Initialize captions only if the audio is played immediately
if !shouldQueueAudio(for: nextCheckpoint) {
if let closedCaptionFile = nextCheckpoint.audio.closedCaption {
print("DEBUG: Initializing closed captions for file: \(closedCaptionFile)")
gameViewController?.initializeClosedCaptions(for: closedCaptionFile)
}
}
// Handle contingencies and update state as before...
}
Helper Method to Determine Queuing:
Add a helper method to determine if the audio should be queued.
3. Ensure Captions Are Managed Only When Audio Plays
With the above changes:
Immediate Playback: Captions are initialized right away.
Queued Playback: Captions are initialized via the callback when the queued audio starts playing.
This ensures that captions are always in sync with the actual audio being played.
Final Thoughts
By implementing callbacks for audio playback start events and adjusting the TourSystem to handle caption initialization based on whether the audio is played immediately or queued, you can maintain synchronization between audio and captions. This approach ensures that captions are only updated when their corresponding audio begins, preventing any mismatch between audio and captions.
Complete Code Snippets
AudioSystem.swift:
swift
System.swift:
swift
Conclusion
By implementing callbacks to synchronize caption initialization with audio playback, you ensure that captions are always in sync with the audio being played. This approach prevents the premature triggering of captions for the next checkpoint, maintaining a consistent and accurate user experience.
If you have any further questions or need additional assistance with this implementation, feel free to ask!
Not yet included in a published release.
import AVFoundation
import AudioKit
import Foundation
enum PlayFileStatus {
case success
case noSuchFile(String)
case error(String)
}
class AudioSystem {
static var shared: AudioSystem = AudioSystem()
private(set) var currentAudioFile: String?
private(set) var currentCaptionFile: String?
private init() {
setupAudioGraph()
}
// MARK: AudioKit Setup
let engine = AudioEngine()
let backgroundAudioNode: AudioPlayer = AudioPlayer()
let foregroundAudioNode = AudioPlayer()
private var _backgroundAudioFile: String = ""
private var _foregroundAudioFile: String = ""
public var backgroundAudioFile: String { _backgroundAudioFile }
public var foregroundAudioFile: String { _foregroundAudioFile }
public var backgroundVolume: Float {
get { backgroundAudioNode.volume }
set {
backgroundAudioNode.volume = newValue
UserDefaults.standard.set(newValue, forKey: Preferences.backgroundVolumeKey)
}
}
private func setupAudioGraph() {
let mainMixerNode = Mixer([foregroundAudioNode, backgroundAudioNode])
mainMixerNode.volume = 2
engine.output = mainMixerNode
foregroundAudioNode.play()
backgroundAudioNode.play()
}
func start() {
do {
try engine.start()
} catch let err {
Log(err)
}
}
func stop() {
engine.stop()
}
func clearBuffer() {
self.playFileInForeground(fileName: "empty.mp3", captionFile: "empty.srt") { }
}
// MARK: Public API Functions
var foregroundAudioDuration: TimeInterval = 0
func pauseForegroundAudio() {
foregroundAudioNode.pause()
}
func pauseBackgroundAudio() {
backgroundAudioNode.pause()
}
func playFileInForeground(fileName: String, captionFile: String?, captionStartClosure: (() -> Void)? = nil) -> PlayFileStatus? {
TourSystem.shared.stopClosedCaptions()
let docDir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let audioFileURL = docDir.appendingPathComponent(fileName, conformingTo: .audio)
do {
let audioFile = try AVAudioFile(forReading: audioFileURL)
let audioFileDuration: TimeInterval = TimeInterval(Double(audioFile.length) / audioFile.fileFormat.sampleRate)
print("Played audio file duration: \(audioFileDuration)")
foregroundAudioDuration = audioFileDuration
foregroundAudioNode.stop()
foregroundAudioNode.reset()
foregroundAudioNode.playerNode.scheduleFile(audioFile, at: AVAudioTime.now(), completionHandler: nil)
foregroundAudioNode.start()
_foregroundAudioFile = fileName
currentAudioFile = fileName
currentCaptionFile = captionFile
foregroundAudioNode.play()
// Initialize captions immediately via closure
captionStartClosure?()
return nil
} catch {
print("Failed to load audio file: \(error)")
_foregroundAudioFile = ""
return .noSuchFile(fileName)
}
}
static func durationOfAudio(audioFileName: String) -> TimeInterval? {
let docDir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let audioFileURL = docDir.appendingPathComponent(audioFileName, conformingTo: .audio)
do {
let audioFile = try AVAudioFile(forReading: audioFileURL)
let audioFileDuration: TimeInterval = TimeInterval(Double(audioFile.length) / audioFile.fileFormat.sampleRate)
return audioFileDuration
} catch {
print("Could not get audio file duration: \(error)")
return nil
}
}
func queueFileInForeground(fileName: String, captionStartClosure: (() -> Void)? = nil) -> PlayFileStatus? {
let docDir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let audioFileURL = docDir.appendingPathComponent(fileName, conformingTo: .audio)
do {
let audioFile = try AVAudioFile(forReading: audioFileURL)
let audioFileDuration: TimeInterval = TimeInterval(Double(audioFile.length) / audioFile.fileFormat.sampleRate)
print("Queued audio file duration: \(audioFileDuration)")
let currentTrackPlaybackTime = foregroundAudioNode.getCurrentTime()
let delay = foregroundAudioDuration - currentTrackPlaybackTime + 0.1
foregroundAudioNode.playerNode.scheduleFile(audioFile, at: AVAudioTime.now().offset(seconds: delay), completionHandler: nil)
if let closure = captionStartClosure {
Timer.scheduledTimer(withTimeInterval: delay, repeats: false) { _ in
closure()
}
}
_foregroundAudioFile = fileName
foregroundAudioDuration += audioFileDuration
foregroundAudioNode.play()
return nil
} catch {
print("Failed to load audio file: \(error)")
_foregroundAudioFile = ""
return .noSuchFile(fileName)
}
}
func playOrQueueFileInForeground(fileName: String, captionFile: String?, captionStartClosure: (() -> Void)? = nil, completion: (() -> Void)? = nil) -> PlayFileStatus? {
let docDir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let audioFileURL = docDir.appendingPathComponent(fileName, conformingTo: .audio)
do {
let audioFile = try AVAudioFile(forReading: audioFileURL)
let audioFileDuration: TimeInterval = TimeInterval(Double(audioFile.length) / audioFile.fileFormat.sampleRate)
let currentTrackPlaybackTime = foregroundAudioNode.getCurrentTime()
if currentTrackPlaybackTime < foregroundAudioDuration {
// Queue the file
foregroundAudioNode.playerNode.scheduleFile(audioFile, at: AVAudioTime.now().offset(seconds: foregroundAudioDuration - currentTrackPlaybackTime + 0.1)) {
DispatchQueue.main.async {
completion?()
}
}
// Schedule the caption initialization
if let closure = captionStartClosure {
Timer.scheduledTimer(withTimeInterval: foregroundAudioDuration - currentTrackPlaybackTime + 0.1, repeats: false) { _ in
closure()
}
}
foregroundAudioDuration += audioFileDuration
} else {
// Play the file immediately
foregroundAudioNode.stop()
foregroundAudioNode.reset()
foregroundAudioNode.playerNode.scheduleFile(audioFile, at: nil) {
DispatchQueue.main.async {
completion?()
}
}
foregroundAudioNode.start()
foregroundAudioDuration = audioFileDuration
// Initialize captions immediately
if let closure = captionStartClosure {
closure()
}
}
_foregroundAudioFile = fileName
currentAudioFile = fileName
currentCaptionFile = captionFile
foregroundAudioNode.play()
return nil
} catch {
print("Failed to load audio file: \(error)")
_foregroundAudioFile = ""
return .noSuchFile(fileName)
}
}
func playFileInBackground(fileName: String) -> PlayFileStatus? {
let docDir = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let audioFileURL = docDir.appendingPathComponent(fileName, conformingTo: .audio)
do {
let audioFile = try AVAudioFile(forReading: audioFileURL)
backgroundAudioNode.stop()
backgroundAudioNode.reset()
backgroundAudioNode.playerNode.scheduleFile(audioFile, at: AVAudioTime.now(), completionHandler: nil)
backgroundAudioNode.start()
_backgroundAudioFile = fileName
return nil
} catch {
print("Failed to load audio file: \(error)")
_backgroundAudioFile = ""
return .noSuchFile(fileName)
}
}
}
import Foundation
class TourSystem {
static let shared = TourSystem()
var state: TourState
var gameViewController: GameViewController?
var endOfTourLogic: (() -> Void)?
// Existing properties and initializations...
func playAudioForCheckpoint(_ checkpoint: Checkpoint) {
let audioSet = checkpoint.audio
print("now playing \(audioSet.foreground)")
var result: PlayFileStatus?
if alwaysQueueAudio {
result = AudioSystem.shared.queueFileInForeground(fileName: audioSet.foreground) {
self.startClosedCaptions(time: 0.0, file: audioSet.closedCaption ?? "")
}
} else if let queueAudio: Bool = checkpoint.queueAudio, queueAudio == true {
result = AudioSystem.shared.queueFileInForeground(fileName: audioSet.foreground) {
self.startClosedCaptions(time: 0.0, file: audioSet.closedCaption ?? "")
}
} else {
AudioSystem.shared.foregroundAudioNode.stop()
AudioSystem.shared.foregroundAudioNode.reset()
result = AudioSystem.shared.playFileInForeground(fileName: audioSet.foreground, captionFile: audioSet.closedCaption) {
self.startClosedCaptions(time: 0.0, file: audioSet.closedCaption ?? "")
}
}
if result != nil {
state.currentForegroundAudio = "Error"
print("failed to play foreground audio due to error \(String(describing: result))")
} else {
state.currentForegroundAudio = audioSet.foreground
print("playing foreground audio file \(audioSet.foreground)")
}
// Handle background audio as before...
}
func nextCheckpoint() {
print("DEBUG: nextCheckpoint called for checkpoint index: \(state.currentCheckpointIndex)")
stopClosedCaptions()
clearClosedCaptions()
timeoutContingencyTimer?.invalidate()
timeoutContingencyTimer = nil
let indexOfCurrentCheckpoint = state.currentCheckpointIndex
if indexOfCurrentCheckpoint >= 0 {
state.completedCheckpoints.append(state.currentPath.checkpoints[indexOfCurrentCheckpoint])
}
guard indexOfCurrentCheckpoint >= -1 else {
print("Invalid checkpoint index.")
return
}
guard indexOfCurrentCheckpoint < state.currentPath.checkpoints.count - 1 else {
print("Last checkpoint in the path reached.")
// Handle end of path logic...
return
}
let nextCheckpointIndex = indexOfCurrentCheckpoint + 1
let nextCheckpoint = state.currentPath.checkpoints[nextCheckpointIndex]
// Play or queue the audio with a caption callback
AudioSystem.shared.playOrQueueFileInForeground(
fileName: nextCheckpoint.audio.foreground,
captionFile: nextCheckpoint.audio.closedCaption
) {
if let closedCaptionFile = nextCheckpoint.audio.closedCaption {
self.startClosedCaptions(time: 0.0, file: closedCaptionFile)
}
}
playAudioForCheckpoint(nextCheckpoint)
stopClosedCaptions()
clearClosedCaptions()
// Initialize captions only if the audio is played immediately
if !shouldQueueAudio(for: nextCheckpoint) {
if let closedCaptionFile = nextCheckpoint.audio.closedCaption {
print("DEBUG: Initializing closed captions for file: \(closedCaptionFile)")
gameViewController?.initializeClosedCaptions(for: closedCaptionFile)
}
}
// Handle contingencies and update state as before...
}
private func shouldQueueAudio(for checkpoint: Checkpoint) -> Bool {
return alwaysQueueAudio || (checkpoint.queueAudio ?? false)
}
// Existing methods...
func startClosedCaptions(time: Double, file: String) {
// Implementation remains the same
}
func stopClosedCaptions() {
// Implementation remains the same
}
func clearClosedCaptions() {
// Implementation remains the same
}
// Other existing methods...
}