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;
}
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
add a comment |
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
I am afraid that this behaviour is not customisable in recent iOS versions.
– Vanya
Nov 17 '18 at 10:07
add a comment |
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
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
objective-c volume ios-bluetooth headphones
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
I am afraid that this behaviour is not customisable in recent iOS versions.
– Vanya
Nov 17 '18 at 10:07