AudioPlayerManager
AudioPlayerManager copied to clipboard
append and prepend are unusable
The AudioPlayerManager class has two public functions that take a queue number/generation as a parameter.
open func prepend(_ audioTracks: [AudioTrack], toQueue generation: Int)
open func append(_ audioTracks: [AudioTrack], toQueue generation: Int)
Inside those two methods a test is made against the current generation or the operation fails. There is no way to determine what a valid value for that might be. The current generation for the manager is a private variable, and none of the methods ever returns the value of that variable.
This makes the prepend and append functions unusable.
There are ~~two~~, ~~three~~, enumerable ways to fix this. Here are a few.
The Easy Way
to make the queueGeneration variable public,
open var queueGeneration
plus: simple fix minus: it doesn't provide whatever protection that check is trying to help in the first place, except to scrupulous programmers plus: it still allows some provided functionality to programmers that want that check to occur
The Hard Way
update every function that touches/changes the generation to return the new generation, the code that called the function that causes the new generation is the code that is allowed to modify the queue
this code that modifies the queue generation value includes:
private func clearQueue()
open func replace(with audioTracks: [AudioTrack], at startIndex: Int)
and indirectly, all other replace and play calls, as well as stop
The Middle Way
add new, additional API for those interested in later calling prepend or (more likely) append, which provides the generation value they need
minus: this is at least 7 different calls currently
Yet More Choice
you could add two additional append/prepend functions that ignore the queue generation, this would allow the same bypass as "The Easy Way" but would not expose the internal value.
open func prepend(_ audioTracks: [AudioTrack])
open func append(_ audioTracks: [AudioTrack])
My Vote
✅ The Easy Way
Hey, thanks for your detailed explanation and solutions!
queueGeneration is used to prevent modifcations on an "old" queue. The track creation is mostly done on a background thread and can takes some time (depended on the tracks). In case e.g. another replace operation is triggered meanwhile, the former append etc. calls should be ignored.
I will dig depper into the issue the upcoming days. My first impression is that the easy way might be good.