Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

DennisBlah

macrumors 6502
Original poster
Dec 5, 2013
485
2
The Netherlands
Hey all, since I'm on this forum I'm having a great help of you guys.
Now I got an question about 'listening' to hardware buttons.
In this example down here I am listening to the Volume up and Down buttons at the side of your phone, which will also listen to the Volume buttons on your iPhone headset.

Code:
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(volumeChanged:)
                                                 name:@"AVSystemController_SystemVolumeDidChangeNotification"
                                               object:nil];

-(IBAction)volumeChanged:(NSNotification *)notification {
    float volume = [[[notification userInfo]
                     objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"]
                    floatValue];
    
    NSLog(@"%.2f", volume);
}

Now I'm wondering how to get an notification about the middle button on my headset. I tried to find different 'AVSystemController_' notifications but without any luck.

When I'm logging the notification userInfo, it wont be triggered by the middle button, only the volume buttons which is kinda logical since I'm checking for ' _SystemVolumeDidChangeNotification'.

Does anyone has any experience with this, or can point me to some sdk which I cant find now ?

Any help would be great!
 
Last edited by a moderator:
I found this

Code:
//view did load
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

//override the canbecomefirstresponder
-(BOOL)canBecomeFirstResponder {
    return YES;
}

- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
    if (receivedEvent.type == UIEventTypeRemoteControl) {
        if (receivedEvent.subtype == UIEventSubtypeRemoteControlTogglePlayPause) {
                NSLog(@"Middle button pressed");
        }
    }
}

This will recognize the middle button..

However, since I got this in, and I go to an different view where I'm playing other music, it will stop right when i played it.

In viewDidDisappear, i putted in
Code:
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
But that didnt do anything with the next view.
I tried to reassign the firstResponder in the next view, but ... nothing.
 
Oww never mind :)
I was doing an timer on the first view about earplugs are in. If FALSE then it will go pause.
So everytime I went to second view, pause will get called every .5 seconds :)


thanks all
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.