'listening' to hardware buttons

Discussion in 'iOS Programming' started by DennisBlah, Feb 13, 2014.

  1. DennisBlah, Feb 13, 2014
    Last edited by a moderator: Feb 13, 2014

    DennisBlah macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #1
    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!
     
  2. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #2
    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.
     
  3. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #3
    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
     

Share This Page