ios - beginReceivingRemoteControlEvents not triggering events for Apple Music -
i playing apple music application , apple music player code -
-(void) submitapplemusictrackwithproductid: (nsstring *) productid // productid in last numbers after i= in share url apple music { [skcloudservicecontroller requestauthorization:^(skcloudserviceauthorizationstatus status) { nslog(@"status %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 icloud!"); [[mpmedialibrary defaultmedialibrary] additemwithproductid:productid completionhandler:^(nsarray<__kindof mpmediaentity *> * _nonnull entities, nserror * _nullable error) { nslog(@"added id%@ entities: %@ , error %@", 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! ability add apple music track 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 block 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]; } }
which working great . wants control track lock screen, did following code in viewwillappear
[[uiapplication sharedapplication] beginreceivingremotecontrolevents]; [app_delegate becomefirstresponder];
and remotecontrolreceivedwithevent
method written in appdelegate
file follows -
-(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; } }
note - remotecontrolreceivedwithevent
triggered every time if itunes
tracks playing avplayer
or spotify
tracks playing spotify ios sdk .
but same code not triggering while playing apple music tracks -
[mpmusicplayercontroller applicationmusicplayer] or [mpmusicplayercontroller systemmusicplayer]
any appreciated. thanks
instead of using -(void)remotecontrolreceivedwithevent:(uievent *)event
track controls, use mpremotecommandcenter
adding targets each 1 of controls:
note: it's important enable controls before assigning target.
[mpremotecommandcenter sharedcommandcenter].playcommand.enabled = yes; [[mpremotecommandcenter sharedcommandcenter].playcommand addtarget:self action:@selector(remoteplay)]; [mpremotecommandcenter sharedcommandcenter].pausecommand.enabled = yes; [[mpremotecommandcenter sharedcommandcenter].pausecommand addtarget:self action:@selector(remotestop)]; [mpremotecommandcenter sharedcommandcenter].previoustrackcommand.enabled = yes; [[mpremotecommandcenter sharedcommandcenter].previoustrackcommand addtarget:self action:@selector(loadprevioussong)]; [mpremotecommandcenter sharedcommandcenter].nexttrackcommand.enabled = yes; [[mpremotecommandcenter sharedcommandcenter].nexttrackcommand addtarget:self action:@selector(loadnextsong)];
selectors:
-(void) remoteplay { [app_delegate playpausemusic:nil]; } -(void) remotestop { [app_delegate playpausemusic:nil]; } -(void) loadnextsong { [app_delegate next:nil]; } -(void) loadprevioussong { [app_delegate previous:nil]; }
Comments
Post a Comment