Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ekilah
Last active November 4, 2021 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ekilah/e74683291d3e7fafb947 to your computer and use it in GitHub Desktop.
Save ekilah/e74683291d3e7fafb947 to your computer and use it in GitHub Desktop.
admob video ads seem to use `[MPRemoteCommandCenter sharedCenter]` and `[MPNowPlayingInfoCenter defaultCenter]`, which messes with apps that play music using the same tools. Using Heyzap Mediation, you can attempt to get around this issue using our network callbacks to know when AdMob fetches and finishes, then manually reset things. shared for h…
// register for heyzap ad network callbacks as early as you can
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(admobCallbackNotification:) name:HZMediationNetworkCallbackNotification object:@"admob"];
- (void) adMobCallbackNotification:(NSNotification *)notif {
NSString *cosnt callback = notif.userInfo[HZNetworkCallbackNameUserInfoKey];
if ([callback isEqualToString:HZNetworkCallbackAvailable] || [callback isEqualToString:HZNetworkCallbackDismiss]) {
[self resetCommandCenter];
[self resetMusicInfo];
}
}
- (void) resetCommandCenter {
MPRemoteCommandCenter * commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
NSArray *commands = @[commandCenter.playCommand, commandCenter.pauseCommand, commandCenter.nextTrackCommand, commandCenter.previousTrackCommand, commandCenter.bookmarkCommand, commandCenter.changePlaybackPositionCommand, commandCenter.changePlaybackRateCommand, commandCenter.dislikeCommand, commandCenter.enableLanguageOptionCommand, commandCenter.likeCommand, commandCenter.ratingCommand, commandCenter.seekBackwardCommand, commandCenter.seekForwardCommand, commandCenter.skipBackwardCommand, commandCenter.skipForwardCommand, commandCenter.stopCommand, commandCenter.togglePlayPauseCommand];
for (MPRemoteCommand *command in commands) {
[command removeTarget:nil];
[command setEnabled:NO];
}
[commandCenter.playCommand addTarget:self action:@selector(tryPlayMusic)];
[commandCenter.pauseCommand addTarget:self action:@selector(tryPauseMusic)];
[commandCenter.playCommand setEnabled:YES];
[commandCenter.pauseCommand setEnabled:YES];
NSLog(@"command center reset");
}
- (void) resetMusicInfo {
MPNowPlayingInfoCenter *infoCenter = [MPNowPlayingInfoCenter defaultCenter];
NSMutableDictionary *infoDict = [NSMutableDictionary dictionary];
infoDict[MPMediaItemPropertyTitle] = @"song title";
infoDict[MPMediaItemPropertyAlbumTitle] = @"Album";
infoDict[MPMediaItemPropertyAlbumArtist] = @"Heyzap";
infoDict[MPMediaItemPropertyPlaybackDuration] = @300;
infoDict[MPNowPlayingInfoPropertyElapsedPlaybackTime] = @27;
infoDict[MPNowPlayingInfoPropertyPlaybackRate] = @1;
NSLog(@"previous info dict: %@", infoCenter.nowPlayingInfo);
infoCenter.nowPlayingInfo = nil;
infoCenter.nowPlayingInfo = infoDict;
NSLog(@"music info reset");
}
@ekilah
Copy link
Author

ekilah commented Dec 10, 2015

For reference, the current constant values (if your framework doesn't allow you to access them for some reason)

NSString * const HZNetworkCallbackAvailable = @"available";
NSString * const HZNetworkCallbackDismiss = @"dismiss";
NSString * const HZMediationNetworkCallbackNotification = @"HZMediationNetworkCallbackNotification";
NSString * const HZNetworkCallbackNameUserInfoKey = @"HZNetworkCallbackNameUserInfoKey";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment