diff --git a/apple/OmnivoreKit/Sources/Services/AudioSession/AudioController.swift b/apple/OmnivoreKit/Sources/Services/AudioSession/AudioController.swift index fdbab92f1..7315fc942 100644 --- a/apple/OmnivoreKit/Sources/Services/AudioSession/AudioController.swift +++ b/apple/OmnivoreKit/Sources/Services/AudioSession/AudioController.swift @@ -78,17 +78,20 @@ class SpeechPlayerItem: AVPlayerItem { resourceLoaderDelegate.owner = self self.observer = observe(\.status, options: [.new]) { item, _ in - item.session.updateDuration(forItem: item.speechItem, newDuration: CMTimeGetSeconds(item.duration)) + if item.status == .readyToPlay { + let duration = CMTimeGetSeconds(item.duration) + item.session.updateDuration(forItem: item.speechItem, newDuration: duration) + } } - NotificationCenter.default.addObserver(forName: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self, queue: OperationQueue.main) { _ in + NotificationCenter.default.addObserver(forName: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self, queue: OperationQueue.main) { [weak self] _ in + guard let self = self else { return } self.completed() } } deinit { - NotificationCenter.default.removeObserver(self) - removeObserver(self, forKeyPath: "status") + observer = nil resourceLoaderDelegate.session?.invalidateAndCancel() } @@ -136,7 +139,6 @@ class SpeechPlayerItem: AVPlayerItem { } // TODO: how do we want to propogate this and handle it in the player - // The exception is just from some old code and does nothing. let audioData = try? await SpeechSynthesizer.download(speechItem: speechItem, session: self.session) DispatchQueue.main.async { self.mediaData = audioData @@ -281,7 +283,7 @@ public class AudioController: NSObject, ObservableObject, AVAudioPlayerDelegate let synthesizer = SpeechSynthesizer(appEnvironment: appEnvironment, networker: networker, document: document) for item in synthesizer.createPlayerItems(from: 0) { do { - _ = try await SpeechSynthesizer.download(speechItem: item) + _ = try await SpeechSynthesizer.download(speechItem: item, redownloadCached: true) } catch { print("error downloading audio segment: ", error) return false @@ -525,6 +527,9 @@ public class AudioController: NSObject, ObservableObject, AVAudioPlayerDelegate setupRemoteControl() } } + if items.count < 1 { + state = .reachedEnd + } } } diff --git a/apple/OmnivoreKit/Sources/Services/AudioSession/SpeechSynthesizer.swift b/apple/OmnivoreKit/Sources/Services/AudioSession/SpeechSynthesizer.swift index 7daf6fe7a..7686be306 100644 --- a/apple/OmnivoreKit/Sources/Services/AudioSession/SpeechSynthesizer.swift +++ b/apple/OmnivoreKit/Sources/Services/AudioSession/SpeechSynthesizer.swift @@ -79,8 +79,10 @@ struct SpeechSynthesizer { } func preload() async throws { - if let item = speechItemForIdx(idx: 0) { - _ = try await Self.download(speechItem: item) + if document.utterances.count > 0 { + if let item = speechItemForIdx(idx: 0) { + _ = try await Self.download(speechItem: item) + } } }