Wednesday, May 22, 2013

beginReceivingRemoteControlEvents example UIApplication ios


[UIApplication beginReceivingRemoteControlEvents]

Tells the application to begin receiving remote-control events.
- (void)beginReceivingRemoteControlEvents
Discussion of [UIApplication beginReceivingRemoteControlEvents]
Remote-control events originate as commands issued by headsets and external accessories that are intended to control multimedia presented by an application. To stop the reception of remote-control events, you must callendReceivingRemoteControlEvents.
Example of [UIApplication beginReceivingRemoteControlEvents]
- (void)viewDidLoad {
    [super viewDidLoad]

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[AVAudioSession sharedInstance] setActive: YES error: nil];

    if ([[UIApplication sharedApplication] respondsToSelector:@selector(beginReceivingRemoteControlEvents)]){
        [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
        [self becomeFirstResponder];
    }

    self.yourPlayer = [[AVAudioPlayer alloc] init];
    self.yourPlayer.delegate = self;
}
Example of [UIApplication beginReceivingRemoteControlEvents]
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    if ([self canBecomeFirstResponder]) {
        [self becomeFirstResponder];
    }
Example of [UIApplication beginReceivingRemoteControlEvents]
- (void)viewDidLoad {
    NSError *setCategoryErr = nil;
    NSError *activationErr  = nil;
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryErr];
    [[AVAudioSession sharedInstance] setActive: YES error: &activationErr];
}

- (void)viewWillAppear {

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


- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
    switch (event.subtype) {
        case UIEventSubtypeRemoteControlPlay:
            [_audio play];
            break;
        case UIEventSubtypeRemoteControlPause:
            [_audio pause];
            break;
        default:
            break;
    }
}