0

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Bhargav Soni
  • 1,067
  • 1
  • 9
  • 22

1 Answers1

0

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.

GeneCode
  • 7,545
  • 8
  • 50
  • 85
  • Do you mean that headphone's volume up/down key can not be customised for certain actions? – Bhargav Soni Nov 19 '18 at 06:22
  • I mean, there is no legal way to do it. There probably is an illegal way to do it using private API (undocumented functions), but you need to search for it. But as I said, Apple app reviewer has automated ways to detect the use of illegal function to your app will be rejected. – GeneCode Nov 20 '18 at 23:28
  • Do you have any specific documentation describing the same provided by Apple. @GeneCode – Anjali jariwala Nov 22 '18 at 06:36
  • @GeneCode check this app, it also works with hardware buttons without affecting system volumes. https://itunes.apple.com/us/app/rallyblitz-nav/id735777925?mt=8 – Bhargav Soni Nov 24 '18 at 04:52
  • the developer of that app just got lucky. You can try your luck. btw, i found solution on SO how to hide the volume HUD: https://stackoverflow.com/questions/24444376/hide-device-volume-hud-view-while-adjusitng-volume-with-mpvolumeview-slider – GeneCode Nov 25 '18 at 03:43