SwiftAudioPlayer icon indicating copy to clipboard operation
SwiftAudioPlayer copied to clipboard

feat(SAPlayer) avoid shared instance

Open kuamanet opened this issue 3 years ago • 0 comments

This is addressing the following:

  • #54
  • #166

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?

kuamanet avatar Sep 16 '22 04:09 kuamanet