Search code examples
iosnotificationsavaudiosession

AVAudioSessionRouteChangeNotification keeps getting called


I'm using AVAudioSession to get info about route changes -

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(currentRouteChanged:)
                                                 name:AVAudioSessionRouteChangeNotification object:nil];

The problem is that after closing the current session -

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    // close down our current session
    [audioSession setActive:NO error:nil];

The notification still getting called.

Currently i'm using -

[[NSNotificationCenter defaultCenter] removeObserver:self];

to stop the callings but it doesn't fill right. If there is no session how come this notification is getting called?

Is there a "stronger" way to kill the audio session instead of -

[audioSession setActive:NO error:nil];

that will get this notification from getting called?

thanks


Solution

  • If there is no session

    There is always an audio session. It is a shared singleton, just like the application, or the notification center or user defaults. Its existence has nothing to do with its activation. The audio session exists from the moment your app launches to the moment it is killed. As the docs say:

    Upon launch, your app automatically gets a singleton audio session

    There is no such thing as "close down the current session". Your idea, to unregister for notifications you no longer want, is correct. Or ignore them when they arrive.