dimitris-c / AudioStreaming

An AudioPlayer/Streaming library for iOS written in Swift using AVAudioEngine.
MIT License
266 stars 56 forks source link

playNextInQueue not working for more than 2 items? #96

Open hell001 opened 1 month ago

hell001 commented 1 month ago

Maybe I'm doing something wrong but this seems like a basic enough example:

let items = [
        URL(string: "<link-to-file1>")!,
        URL(string: "<link-to-file2>")!,
    URL(string: "<link-to-file3>")!
]

then I have a button with the following action:

player.queue(urls: items) // player is AudioPlayer()

this plays file 1

I have another button that calls the following:

player.playNextInQueue()

this jumps to file 2

then I call (again)

player.playNextInQueue()

but this does nothing and file 2 continues playing

has this function not been tested/used much or am I using it wrong?


update 1:

so if I just queue the songs and let them play, they play correctly. if I queue the songs and skip to file 2, and let file 2 finish, it does NOT go on to file 3. so regardless of that second playNextInQueue call above, the issue happens when I call It the first time


update 2:

it seems that playNextInQueue calls this:

playerContext.setInternalState(to: .pendingNext)

which will make its way to calling processSource at some point, and in processSource it has this:

if playerContext.internalState == .pendingNext {
            let entry = entriesQueue.dequeue(type: .upcoming)
            playerContext.setInternalState(to: .waitingForData)
            setCurrentReading(entry: entry, startPlaying: true, shouldClearQueue: true)
            rendererContext.resetBuffers()
}

specifically, shouldClearQueue is set to true, so the queue is being emptied. changing it from true to false makes it work, but im not sure if this breaks any other functionality, so id prefer to wait to see what anyone has to say


update 3:

it seems the only other time the internal state is set to pendingNext is in the play(audioEntry: AudioEntry) function (someone double check me), which already clears the queue as shown below:

    private func play(audioEntry: AudioEntry) {
        // ... code ...
        serializationQueue.sync {
            clearQueue()
            entriesQueue.enqueue(item: audioEntry, type: .upcoming)
            playerContext.setInternalState(to: .pendingNext)
             // ... code ...
        }
         // ... code ...
    }

so it SEEMS that changing shouldClearQueue in the above update to false wouldn't break it since it already clears the queue (in fact it seems that in its current form the queue is being cleared twice - once in play() and the other in processSource)? however im not sure how this may impact functionality that may be added in the future, so again I will wait to hear back if its a simple boolean value change

dimitris-c commented 1 month ago

Hey, so the playNextInQueue accidentally made it to release, probably due to an incorrect merge. It wasn't ready for production, apologies for this, will have to remove it