From ff20c4dd6ab7c0b78ddb857100681baa8e47a449 Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Tue, 28 Oct 2025 22:30:05 +0100 Subject: [PATCH 1/2] Allow access the upcoming queue items, and inserting upcoming items after a particular item --- .../Streaming/AudioPlayer/AudioPlayer.swift | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/AudioStreaming/Streaming/AudioPlayer/AudioPlayer.swift b/AudioStreaming/Streaming/AudioPlayer/AudioPlayer.swift index 6204639..f6e0950 100644 --- a/AudioStreaming/Streaming/AudioPlayer/AudioPlayer.swift +++ b/AudioStreaming/Streaming/AudioPlayer/AudioPlayer.swift @@ -91,6 +91,12 @@ open class AudioPlayer { return entry.framesPlayed } + public var upcomingItems: [AudioEntryId] { + serializationQueue.sync { + return entriesQueue.items(type: .upcoming).map { $0.id } + } + } + public private(set) var customAttachedNodes = [AVAudioNode]() /// The current configuration of the player. @@ -287,7 +293,7 @@ open class AudioPlayer { /// - parameter source: A `CoreAudioStreamSource` that will providing streaming data /// - parameter entryId: A `String` that provides a unique id for this item /// - parameter format: An `AVAudioFormat` the format of this audio source - public func queue(source: CoreAudioStreamSource, entryId: String, format: AVAudioFormat) { + public func queue(source: CoreAudioStreamSource, entryId: String, format: AVAudioFormat, after afterUrl: URL? = nil) { let audioEntry = AudioEntry(source: source, entryId: AudioEntryId(id: entryId), outputAudioFormat: format) queue(audioEntry: audioEntry) } @@ -352,6 +358,23 @@ open class AudioPlayer { } } + private func queue(audioEntry: AudioEntry, after afterId: String? = nil) { + serializationQueue.sync { + audioEntry.delegate = self + if let afterId = afterUrl { + if let afterUrlEntry = entriesQueue.items(type: .upcoming).first(where: { $0.id.id == afterUrl.absoluteString }) { + entriesQueue.insert(item: audioEntry, type: .upcoming, after: afterUrlEntry) + } + } else { + entriesQueue.enqueue(item: audioEntry, type: .upcoming) + } + } + checkRenderWaitingAndNotifyIfNeeded() + sourceQueue.async { [weak self] in + self?.processSource() + } + } + /// Stops the audio playback public func stop(clearQueue: Bool = true) { guard playerContext.internalState != .stopped else { return } From e1273d3d7eb35e28da394a80e600183ab04abd53 Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Tue, 28 Oct 2025 22:38:41 +0100 Subject: [PATCH 2/2] Use single method --- .../Streaming/AudioPlayer/AudioPlayer.swift | 23 +++---------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/AudioStreaming/Streaming/AudioPlayer/AudioPlayer.swift b/AudioStreaming/Streaming/AudioPlayer/AudioPlayer.swift index f6e0950..059b122 100644 --- a/AudioStreaming/Streaming/AudioPlayer/AudioPlayer.swift +++ b/AudioStreaming/Streaming/AudioPlayer/AudioPlayer.swift @@ -320,7 +320,7 @@ open class AudioPlayer { /// - parameter headers: A `Dictionary` specifying any additional headers to be pass to the network request. public func queue(url: URL, headers: [String: String], after afterUrl: URL? = nil) { let audioEntry = entryProvider.provideAudioEntry(url: url, headers: headers) - queue(audioEntry: audioEntry, after: afterUrl) + queue(audioEntry: audioEntry, after: afterUrl?.absoluteString) } /// Queues the specified URLs @@ -341,28 +341,11 @@ open class AudioPlayer { } } - private func queue(audioEntry: AudioEntry, after afterUrl: URL? = nil) { - serializationQueue.sync { - audioEntry.delegate = self - if let afterUrl = afterUrl { - if let afterUrlEntry = entriesQueue.items(type: .upcoming).first(where: { $0.id.id == afterUrl.absoluteString }) { - entriesQueue.insert(item: audioEntry, type: .upcoming, after: afterUrlEntry) - } - } else { - entriesQueue.enqueue(item: audioEntry, type: .upcoming) - } - } - checkRenderWaitingAndNotifyIfNeeded() - sourceQueue.async { [weak self] in - self?.processSource() - } - } - private func queue(audioEntry: AudioEntry, after afterId: String? = nil) { serializationQueue.sync { audioEntry.delegate = self - if let afterId = afterUrl { - if let afterUrlEntry = entriesQueue.items(type: .upcoming).first(where: { $0.id.id == afterUrl.absoluteString }) { + if let afterId { + if let afterUrlEntry = entriesQueue.items(type: .upcoming).first(where: { $0.id.id == afterId }) { entriesQueue.insert(item: audioEntry, type: .upcoming, after: afterUrlEntry) } } else {