dimitris-c / AudioStreaming

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

Paused audio plays for a short time before starts playing new audio #68

Closed amralharazi closed 8 months ago

amralharazi commented 8 months ago

First of all, thank you for this library. It really did help me starting playing audios faster than using Apple's out-of-the-box solution.

But, I've just noticed an issue, not sure if that's something on my side. When an audio is paused, then another audio is set to be played, the previous audio starts playing for a faction of a second then the new audio starts playing.

I was in a hurry, so after some trial and error, I figured that stopping the player and changing the play(url: URL, headers: [String: String]) method in AudioPlayer to the following did the trick , but that's not a perfect solution for sure.

    public func play(url: URL, headers: [String: String]) {
        let audioEntry = entryProvider.provideAudioEntry(url: url, headers: headers)
        audioEntry.delegate = self

        checkRenderWaitingAndNotifyIfNeeded()
        sourceQueue.async { [weak self] in
            guard let self = self else { return }

            clearQueue()
            entriesQueue.enqueue(item: audioEntry, type: .upcoming)
            playerContext.setInternalState(to: .pendingNext)
            do {
                try self.startEngineIfNeeded()
            } catch {
                self.raiseUnexpected(error: .audioSystemError(.engineFailure))
            }
            self.processSource()
        }
    }

Thank you again and I'd appreciate your feedback to this issue.

dimitris-c commented 8 months ago

Thanks for reporting this. will have a look

dimitris-c commented 8 months ago

This is fixed on the main branch, a new release will be up soon with this fix.