dimitris-c / AudioStreaming

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

Do not deactivate the current audio session on instantiating #49

Closed haecksenwerk closed 1 year ago

haecksenwerk commented 1 year ago

When instantiating AudioPlayer() , the currently activated AVAudioSession is deactivated. So, when starting the app, it stops the playback from other apps. This is not how apps like Apple Music or Podcasts behave.

Please have AudioPlayer() not close the current Audio-Sessions during initialization, so that one can instead activate it before calling the play() command.

dimitris-c commented 1 year ago

Apart for the Example project, the library itself does not contain any code in regards to AVAudioSession configuration etc.

AVAudioSession configuration is entirely up to whoever chooses to use this library. You can read about AVAudioSession here https://developer.apple.com/documentation/avfaudio/avaudiosession

haecksenwerk commented 1 year ago

Right, this is what I do the same as in the example project, setting AVAudioSession.sharedInstance().setActive to true when start playing audio and to false when stop playing. This is why I was suspecting that maybe during the Audio Core initialization the current audio session would be set inactive for some reason.

But the real problem is a different one:

I have stripped everything related to audio from my code now, I only do a var player = AudioPlayer()at program start, which alone leads to stopping any current playing audio from other apps then.

Do I have to configure something before I instantiate AudioPlayer() or could there be potentially something happening during the librarie's audio core init process that shuts down the audio which is currently played by other apps?

junyaninflection commented 1 year ago

I encountered the same issue as @haecksenwerk , it's unclear to me which part of the initialization code inside AudioPlayer() caused background audio to stop. @haecksenwerk did you get a chance to look into that? if not I'll do some further debugging.

iDevelopper commented 1 year ago

This is the line (153) in AudioPlayer.swift:

        frameFilterProcessor = FrameFilterProcessor(mixerNode: audioEngine.mainMixerNode)

We should replace it with:

        frameFilterProcessor = FrameFilterProcessor(mixerNode: AVAudioMixerNode())
dimitris-c commented 1 year ago

@iDevelopper Replacing that would mean the filtering will not apply on the playing audio though...

haecksenwerk commented 1 year ago

@junyaninflection I haven't looked into the issue any further

junyaninflection commented 1 year ago

@dimitris-c @iDevelopper @haecksenwerk could you review if this PR make sense ? @iDevelopper does pinpoint the root cause and all I'm doing in this PR is deferring the access of mainMixerNode. I verified that it resolves the issue