beginReceivingRemoteControlEvents no desencadena eventos para Apple Music
Estoy reproduciendo Apple Music desde mi aplicación, el código del reproductor de música de Apple es como:
-(void) submitAppleMusicTrackWithProductID: (NSString *) productID // productID in US is the last numbers after i= in the share URL from Apple Music
{
[SKCloudServiceController requestAuthorization:^(SKCloudServiceAuthorizationStatus status) {
NSLog(@"status is %ld", (long)status);
SKCloudServiceController *cloudServiceController;
cloudServiceController = [[SKCloudServiceController alloc] init];
[cloudServiceController requestCapabilitiesWithCompletionHandler:^(SKCloudServiceCapability capabilities, NSError * _Nullable error) {
NSLog(@"%lu %@", (unsigned long)capabilities, error);
if (capabilities >= SKCloudServiceCapabilityAddToCloudMusicLibrary || capabilities==SKCloudServiceCapabilityMusicCatalogPlayback)
{
NSLog(@"You CAN add to iCloud!");
[[MPMediaLibrary defaultMediaLibrary] addItemWithProductID:productID completionHandler:^(NSArray<__kindof MPMediaEntity *> * _Nonnull entities, NSError * _Nullable error)
{
NSLog(@"added id%@ entities: %@ and error is %@", productID, entities, error);
NSArray *tracksToPlay = [NSArray arrayWithObject:productID];
[[MPMusicPlayerController applicationMusicPlayer] setQueueWithStoreIDs:tracksToPlay];
[[MPMusicPlayerController applicationMusicPlayer] stop];
[[MPMusicPlayerController applicationMusicPlayer] play];
self.isTrackChangedByNextPreviousButton = NO;
[[NSNotificationCenter defaultCenter]removeObserver:self name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter]removeObserver:self name:MPMusicPlayerControllerPlaybackStateDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleTrackChanged:)
name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handlePlaybackStateChanged:)
name:MPMusicPlayerControllerPlaybackStateDidChangeNotification
object:nil];
[[MPMusicPlayerController applicationMusicPlayer] beginGeneratingPlaybackNotifications];
[[MPMusicPlayerController applicationMusicPlayer] setRepeatMode: MPMusicRepeatModeNone];
}];
}
else
{
NSLog(@"Blast! The ability to add Apple Music track is not there. sigh.");
}
}];
}];
}
-(void)handleTrackChanged:(id )notification
{
if (!self.AppleMusicPreviuosTrack)
{
self.AppleMusicPreviuosTrack = [[Tracks alloc] init];
}
if (self.AppleMusicPreviuosTrack.trackId == self.CurrentTrack.trackId && [MPMusicPlayerController applicationMusicPlayer].currentPlaybackRate == 0 && !self.isSongChangedManually)
{
self.isSongChangedManually = YES;
[self FilterArtistsTracks:@"next" :^(Tracks *track, NSError *err)
{
}];
}
else
{
if ([[MPMusicPlayerController applicationMusicPlayer] currentPlaybackRate]==1)
{
self.AppleMusicPreviuosTrack.trackId = self.CurrentTrack.trackId;
[[NSNotificationCenter defaultCenter] postNotificationName:kTrackChanged object:nil];
//Delay execution of my block for 20 seconds.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 30 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
self.isSongChangedManually = NO;
});
}
}
}
-(void)handlePlaybackStateChanged:(id )notification
{
NSLog(@"handle_PlaybackStateChanged");
[[NSNotificationCenter defaultCenter] postNotificationName:kDidTrackPlaybackStatus object:nil];
if ([MPMusicPlayerController applicationMusicPlayer].currentPlaybackRate>0)
{
[self.playerMenuView.playpauseButton setImage:[UIImage imageNamed:@"pause"] forState:UIControlStateNormal];
}
else
{
[self.playerMenuView.playpauseButton setImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];
}
}
que está funcionando muy bien Ahora quiero controlar la pista desde la pantalla de bloqueo, para esto hice el siguiente código enviewWillAppear
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[APP_DELEGATE becomeFirstResponder];
yremoteControlReceivedWithEvent
El método está escrito enAppDelegate
presentar de la siguiente manera:
-(void)remoteControlReceivedWithEvent:(UIEvent *)event
{
switch (event.subtype)
{
case UIEventSubtypeRemoteControlTogglePlayPause:
[APP_DELEGATE PlayPauseMusic:nil];
//[streamer pause];
break;
case UIEventSubtypeRemoteControlPlay:
[APP_DELEGATE PlayPauseMusic:nil];
//[streamer start];
break;
case UIEventSubtypeRemoteControlPause:
[APP_DELEGATE PlayPauseMusic:nil];
//[streamer pause];
break;
case UIEventSubtypeRemoteControlStop:
[APP_DELEGATE PlayPauseMusic:nil];
//[streamer stop];
break;
case UIEventSubtypeRemoteControlNextTrack:
[APP_DELEGATE next:nil];
break;
case UIEventSubtypeRemoteControlPreviousTrack:
[APP_DELEGATE previous:nil];
break;
default:
break;
}
}
Nota - losremoteControlReceivedWithEvent
activado cada vez siiTunes
las pistas están siendo reproducidas porAVPlayer
OSpotify
las pistas están siendo reproducidas por Spotify iOS SDK.
Pero el mismo código no se activa al reproducir pistas de Apple Music al:
[MPMusicPlayerController applicationMusicPlayer]
or
[MPMusicPlayerController systemMusicPlayer]
Cualquier ayuda será apreciada. Gracias