MPRemoteCommandCenter calls the handler block multiple times and causes unnecessary calls to selector methods.
Here is code snippet:
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
[commandCenter.nextTrackCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
NSLog(@"NEXTTTTTT");
return MPRemoteCommandHandlerStatusSuccess;
}];
[commandCenter.previousTrackCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
NSLog(@"PREVIOUSSS");
return MPRemoteCommandHandlerStatusSuccess;
}];
When user clicks on next or previous button from the music player dock while screen is locked it causes multiple times to call the above blocks.
The handler will be called as many times as it is added, even if it is registered on the same object multiple times. Perhaps your code snippet is called more than once.
It looks like you have multiple instances of the object you call your code, eg. if your pushing a new UIViewController per track. The old view controller might still exist and call the handler again.
Try to put your code in
- (void)viewDidAppear:(BOOL)animated
and then disable it like this
- (void)viewWillDisappear:(BOOL)animated {
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
[commandCenter.nextTrackCommand removeTarget:self];
[commandCenter.previousTrackCommand removeTarget:self];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With