22

I'm having problem to control the iPhone controls with my avplayer.

if I put the function

- (void)remoteControlReceivedWithEvent:(UIEvent *)event

in the view controller that responsible for playing the function called but only if I i'm going to background in the current view controller.

if i'm going to background from other view controller the function never called.

that's why i want to put it in the app delegate.

I tried Becomefirstresponse and to put the function in every view controller but it did help.

also I call

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

in the

-(void)applicationWillResignActive:(UIApplication *)application

thanks

Janub
  • 1,594
  • 14
  • 26

3 Answers3

24

I have used below code to iPhone Control -

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];

Used to get register for listening the remote control. Once done remove it -

[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];

make the App canBecomeFirstResponder-

- (BOOL)canBecomeFirstResponder {
    return YES;
}

Used delegate method to handle iPhone control, like play and pause while doble tap on the home button

- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
    //if it is a remote control event handle it correctly
    if (event.type == UIEventTypeRemoteControl) {
        if (event.subtype == UIEventSubtypeRemoteControlPlay) {
           [audioPlayer play];
            NSLog(@"play");
        } else if (event.subtype == UIEventSubtypeRemoteControlPause) {
           [audioPlayer stop];
             NSLog(@"pause");
        } else if (event.subtype == UIEventSubtypeRemoteControlTogglePlayPause) {
            NSLog(@"toggle");
        }
    }
}

In my case i am able to handle play and pause.Please let know if any thing wrong.

Sanoj Kashyap
  • 5,020
  • 4
  • 49
  • 75
  • did not check for iOS 6+ , will check. – Sanoj Kashyap Jul 24 '13 at 05:34
  • Sorry , last from that day i have not tested.But i guess it will work. – Sanoj Kashyap Jan 09 '14 at 06:25
  • guys, this is works. Just don't forget to override - (BOOL)canBecomeFirstResponder {} method – Jen Jose Apr 29 '17 at 06:12
  • Hey Sandy, I did as instructed. Added becomeFirstResponder in viewDidAppear() and resignFirstResponder in viewDidDisappear(). Also, implemented canBecomeFirstResponder method. However, when I press any button on the UI, I am not receiving any event in remoteControlReceivedWithEvent, i.e. the method is not getting called. Any pointers on what I might be missing? – A_G May 12 '17 at 11:55
  • http://stackoverflow.com/questions/43973602/unable-to-receive-remotecontrolreceivedwithevent-objective-c-ios – A_G May 15 '17 at 10:45
12

You can move the function up the responder chain, to UIApplication subclass. This way, it will always be there to catch the event.

This kind of event is ignored in common UI and controller classes, so it travels up to the bottom of responder chain, where your app delegate and the the application itself reside.

As noted here, UIApplication's delegate is not part of responder chain (I was wrong here). UIApplication is there, so is root UIWidow, all the views in chain and corresponding UIViewControllers.

Farcaller
  • 3,070
  • 1
  • 27
  • 42
  • 1
    I tried to add the fuction to the app delegate but not luck with that. finally i created a subclass of uiwindow and implemented the function there – Janub Aug 13 '12 at 07:35
  • 1
    @iYaniv It's a better idea to subclass UIApplication to catch such events. You can have more than one window :) – fbernardo Aug 17 '12 at 11:08
2

Hint: becomeFirstResponder must be called from within viewDidAppear, not viewWillAppear...

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self becomeFirstResponder];
}
OhadM
  • 4,687
  • 1
  • 47
  • 57
Sascha Reuter
  • 355
  • 2
  • 7