iOS custom action for volume up/down from Headphones





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







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.










share|improve this question

























  • I am afraid that this behaviour is not customisable in recent iOS versions.

    – Vanya
    Nov 17 '18 at 10:07


















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.










share|improve this question

























  • I am afraid that this behaviour is not customisable in recent iOS versions.

    – Vanya
    Nov 17 '18 at 10:07














0












0








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.










share|improve this question
















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.







objective-c volume ios-bluetooth headphones






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 17 '18 at 16:15









rmaddy

248k27329394




248k27329394










asked Nov 17 '18 at 6:34









Bhargav SoniBhargav Soni

721522




721522













  • I am afraid that this behaviour is not customisable in recent iOS versions.

    – Vanya
    Nov 17 '18 at 10:07



















  • I am afraid that this behaviour is not customisable in recent iOS versions.

    – Vanya
    Nov 17 '18 at 10:07

















I am afraid that this behaviour is not customisable in recent iOS versions.

– Vanya
Nov 17 '18 at 10:07





I am afraid that this behaviour is not customisable in recent iOS versions.

– Vanya
Nov 17 '18 at 10:07












1 Answer
1






active

oldest

votes


















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.






share|improve this answer
























  • Do you mean that headphone's volume up/down key can not be customised for certain actions?

    – Bhargav Soni
    Nov 19 '18 at 6: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 6:36











  • @GeneCode check this app, it also works with hardware buttons without affecting system volumes. itunes.apple.com/us/app/rallyblitz-nav/id735777925?mt=8

    – Bhargav Soni
    Nov 24 '18 at 4: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: stackoverflow.com/questions/24444376/…

    – GeneCode
    Nov 25 '18 at 3:43












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348862%2fios-custom-action-for-volume-up-down-from-headphones%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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.






share|improve this answer
























  • Do you mean that headphone's volume up/down key can not be customised for certain actions?

    – Bhargav Soni
    Nov 19 '18 at 6: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 6:36











  • @GeneCode check this app, it also works with hardware buttons without affecting system volumes. itunes.apple.com/us/app/rallyblitz-nav/id735777925?mt=8

    – Bhargav Soni
    Nov 24 '18 at 4: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: stackoverflow.com/questions/24444376/…

    – GeneCode
    Nov 25 '18 at 3:43
















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.






share|improve this answer
























  • Do you mean that headphone's volume up/down key can not be customised for certain actions?

    – Bhargav Soni
    Nov 19 '18 at 6: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 6:36











  • @GeneCode check this app, it also works with hardware buttons without affecting system volumes. itunes.apple.com/us/app/rallyblitz-nav/id735777925?mt=8

    – Bhargav Soni
    Nov 24 '18 at 4: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: stackoverflow.com/questions/24444376/…

    – GeneCode
    Nov 25 '18 at 3:43














0












0








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.






share|improve this answer













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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 18 '18 at 9:43









GeneCodeGeneCode

5,70053565




5,70053565













  • Do you mean that headphone's volume up/down key can not be customised for certain actions?

    – Bhargav Soni
    Nov 19 '18 at 6: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 6:36











  • @GeneCode check this app, it also works with hardware buttons without affecting system volumes. itunes.apple.com/us/app/rallyblitz-nav/id735777925?mt=8

    – Bhargav Soni
    Nov 24 '18 at 4: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: stackoverflow.com/questions/24444376/…

    – GeneCode
    Nov 25 '18 at 3:43



















  • Do you mean that headphone's volume up/down key can not be customised for certain actions?

    – Bhargav Soni
    Nov 19 '18 at 6: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 6:36











  • @GeneCode check this app, it also works with hardware buttons without affecting system volumes. itunes.apple.com/us/app/rallyblitz-nav/id735777925?mt=8

    – Bhargav Soni
    Nov 24 '18 at 4: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: stackoverflow.com/questions/24444376/…

    – GeneCode
    Nov 25 '18 at 3:43

















Do you mean that headphone's volume up/down key can not be customised for certain actions?

– Bhargav Soni
Nov 19 '18 at 6:22





Do you mean that headphone's volume up/down key can not be customised for certain actions?

– Bhargav Soni
Nov 19 '18 at 6: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





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 6:36





Do you have any specific documentation describing the same provided by Apple. @GeneCode

– Anjali jariwala
Nov 22 '18 at 6:36













@GeneCode check this app, it also works with hardware buttons without affecting system volumes. itunes.apple.com/us/app/rallyblitz-nav/id735777925?mt=8

– Bhargav Soni
Nov 24 '18 at 4:52





@GeneCode check this app, it also works with hardware buttons without affecting system volumes. itunes.apple.com/us/app/rallyblitz-nav/id735777925?mt=8

– Bhargav Soni
Nov 24 '18 at 4: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: stackoverflow.com/questions/24444376/…

– GeneCode
Nov 25 '18 at 3:43





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: stackoverflow.com/questions/24444376/…

– GeneCode
Nov 25 '18 at 3:43




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53348862%2fios-custom-action-for-volume-up-down-from-headphones%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python