tanhakabir / SwiftAudioPlayer

Streaming and realtime audio manipulation with AVAudioEngine
https://medium.com/chameleon-podcast/creating-an-advanced-streaming-audio-engine-for-ios-9fbc7aef4115
MIT License
544 stars 107 forks source link

feat(SAPlayer) avoid shared instance #171

Closed kuamanet closed 1 year ago

kuamanet commented 1 year ago

This is addressing the following:

Basically I've removed the shared instance and made the SAPlayer public. This allows whoever uses the library to play simultaneously more than one audio file. The caveat is that the AVAudioEngine must be created outside of the library.

let engine = AVAudioEngine()
let player1 = SAPlayer(engine: engine)
let player2 = SAPlayer(engine: engine)

I've played around with the demo, and everything seems to keep working as before. I'm sorry for the big diff, I launched a swiftformat inside the project.

Only question, I commented the pause on the numberOfBuffersScheduledInTotal#didSet

private var numberOfBuffersScheduledInTotal = 0 {
        didSet {
            Log.debug("number of buffers scheduled in total: \(numberOfBuffersScheduledInTotal)")
            if numberOfBuffersScheduledInTotal == 0 {
                if playingStatus == .playing { wasPlaying = true }
                pause()
                // Pausing here triggers an odd state where, while downloading the audio the player will not resume playing when the first buffer is ready
//                pause()
                //                delegate?.didError()
                // TODO: we should not have an error here. We should instead have the throttler
                // propegate when it doesn't enough buffers while they were playing
                // TODO: "Make this a legitimate warning to user about needing more data from stream"
            }

            if numberOfBuffersScheduledInTotal > MIN_BUFFERS_TO_BE_PLAYABLE && wasPlaying {

            if numberOfBuffersScheduledInTotal > MIN_BUFFERS_TO_BE_PLAYABLE, wasPlaying {
                wasPlaying = false
                play()
            }
        }
    }

Nothing seems to change. What was that pause() guarding?