Requirement: When a user plugs in headphone with iPhone through wire/wireless having volume up/down keys available in same, user can able to perform custom actions through that buttons. (Do not need to change system volumes from headphones)
Current implementation:
To determine whether the headphones are plugged in:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleRouteChange:) name:AVAudioSessionRouteChangeNotification object:[AVAudioSession sharedInstance]];
- (void)handleRouteChange:(NSNotification *)notification
{
NSDictionary *dicUserInfo = notification.userInfo;
uint reason = [dicUserInfo[AVAudioSessionRouteChangeReasonKey] intValue];
BOOL isHeadphonesConnected = NO;
switch (reason)
{
case AVAudioSessionRouteChangeReasonNewDeviceAvailable:
{
AVAudioSession *session = [AVAudioSession sharedInstance];
for (AVAudioSessionPortDescription *output in session.currentRoute.outputs)
{
if ([output.portType isEqualToString:AVAudioSessionPortHeadphones])
{
isHeadphonesConnected = YES;
}
}
}
break;
case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
{
AVAudioSessionRouteDescription *previousRoute = dicUserInfo[AVAudioSessionRouteChangePreviousRouteKey];
for (AVAudioSessionPortDescription *output in previousRoute.outputs)
{
if ([output.portType isEqualToString:AVAudioSessionPortHeadphones])
{
isHeadphonesConnected = NO;
}
}
}
break;
default:
break;
}
if (isHeadphonesConnected)
{
NSLog(@"Headphones connected");
}
else
{
NSLog(@"Headphones disconnected");
}
}
To determine whether volume key has been pressed from headphones:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(volumeDidChange:) name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil];
- (IBAction)volumeDidChange:(NSNotification *)notification
{
CGFloat newVolume = [[notification.userInfo valueForKey:@"AVSystemController_AudioVolumeNotificationParameter"] floatValue];
if (volume == 0 || newVolume < volume)
{
// change value
}
else
{
// change value
}
volume = newVolume;
}
Issues we are facing from current implementation:
We are facing issue that whenever user clicks volume button from headphone we do able to trigger volumeDidChange
method, but it also changes the system volume and shows volume HUD alert, which we do not need to change. System volume should be constant.
If user clicks on device volume up and down button, it can change system volume but from headphones it should only perform certain actions only.
Answers in Swift language will also be appreciated.
iOS is very strict that you cannot change intended behaviour of a hardware buttons. Your app will be rejected even you are able to do it.