How can we handle headphone play/pause button events in foreground in ios

Santosh Gurram picture Santosh Gurram · Mar 15, 2013 · Viewed 7.5k times · Source

I have an requirement that to handle headphone play/pause button events in foreground. How ever I am able to handle the same scenario in background using the below code

if ([[UIApplication sharedApplication] respondsToSelector:@selector(beginReceivingRemoteControlEvents)]){
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];
    [self becomeFirstResponder];
    NSLog(@"Responds!");
}

Please help with an explanation or sample code if possible. I have done lots of research but no help.

Answer

slobodans picture slobodans · Jul 27, 2015

There is another way to implement player control from headphones. use MPRemoteCommandCenter tooglePlayPauseCommand. Apple documentation

[[MPRemoteCommandCenter sharedCommandCenter].togglePlayPauseCommand addTarget:self action:@selector(onTooglePlayPause)];