objective c - iOS MediaPlayer Remote control events not working -
i'am trying add remote control events music player app, can't working.
some facts:
- i'm using
[mpmusicplayercontroller applicationmusicplayer];
- i added audio, airplay , picture in picture background modes;
- the example playing audio;
- it not receive remote notifications lock screen , control center;
i've made example project can downloaded here: example project main source:
#import "viewcontroller.h" #import <mediaplayer/mpmediapickercontroller.h> #import <mediaplayer/mpmediaquery.h> #import <mediaplayer/mediaplayer.h> @interface viewcontroller () @property (strong, nonatomic) mpmusicplayercontroller *musicplayer; @property (strong, nonatomic) mpmediaitemcollection *musicplayersongs; @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; _musicplayersongs = [mpmediaitemcollection alloc]; _musicplayer = [mpmusicplayercontroller applicationmusicplayer]; [_musicplayer setshufflemode: mpmusicshufflemodeoff]; [_musicplayer setrepeatmode: mpmusicrepeatmodenone]; [_musicplayer begingeneratingplaybacknotifications]; } -(void)viewdidappear:(bool)animated{ [super viewdidappear:animated]; nsmutablearray *selectedtracks = [nsmutablearray new]; //find tracks contains 'a' mpmediapropertypredicate *songpredicate = [mpmediapropertypredicate predicatewithvalue:@"a" forproperty:mpmediaitempropertytitle comparisontype:mpmediapredicatecomparisoncontains]; mpmediaquery *mediaquery = [[mpmediaquery alloc] init]; [mediaquery addfilterpredicate:songpredicate]; [selectedtracks addobjectsfromarray:[mediaquery items]]; nslog(@"number of tracks containing 'a': %lu",(unsigned long)selectedtracks.count); self.musicplayersongs = [[mpmediaitemcollection alloc]initwithitems:selectedtracks]; [self.musicplayer setqueuewithitemcollection:self.musicplayersongs]; [self.musicplayer play]; [self.musicplayer begingeneratingplaybacknotifications]; [self basicsetup]; [self updatenowplayingcenter]; } - (void)basicsetup { //listen remote control events [mpremotecommandcenter sharedcommandcenter].previoustrackcommand.enabled = no; [mpremotecommandcenter sharedcommandcenter].nexttrackcommand.enabled = no; [mpremotecommandcenter sharedcommandcenter].playcommand.enabled = yes; [mpremotecommandcenter sharedcommandcenter].pausecommand.enabled = yes; [mpremotecommandcenter sharedcommandcenter].toggleplaypausecommand.enabled = yes; [[mpremotecommandcenter sharedcommandcenter].nexttrackcommand addtarget:self action:@selector(remotenext)]; [[mpremotecommandcenter sharedcommandcenter].previoustrackcommand addtarget:self action:@selector(remoteprevious)]; [[mpremotecommandcenter sharedcommandcenter].playcommand addtarget:self action:@selector(remoteplay)]; [[mpremotecommandcenter sharedcommandcenter].pausecommand addtarget:self action:@selector(remoteplay)]; [[mpremotecommandcenter sharedcommandcenter].toggleplaypausecommand addtarget:self action:@selector(remotetoggleplaystate)]; [[uiapplication sharedapplication] beginreceivingremotecontrolevents]; } -(void)remoteplay{ nslog(@"remoteplay"); [self.musicplayer play]; } -(void)remotenext{ nslog(@"remotenext"); [self.musicplayer skiptonextitem]; [self updatenowplayingcenter]; } -(void)remoteprevious{ nslog(@"remoteprevious"); [self.musicplayer skiptopreviousitem]; [self updatenowplayingcenter]; } -(void)remotepause{ nslog(@"remotepause"); [self.musicplayer pause]; } -(void)remotetoggleplaystate{ nslog(@"remotetoggleplaystate"); if([self.musicplayer playbackstate] == mpmusicplaybackstateplaying){ [self.musicplayer pause]; }else{ [self.musicplayer play]; } } -(void)updatenowplayingcenter{ mpnowplayinginfocenter *center = [mpnowplayinginfocenter defaultcenter]; nsmutabledictionary *songinfo = [nsmutabledictionary dictionarywithdictionary:@{ mpmediaitempropertyartist: [self.musicplayer nowplayingitem].artist, mpmediaitempropertytitle: [self.musicplayer nowplayingitem].title, mpmediaitempropertyalbumtitle: [self.musicplayer nowplayingitem].albumtitle, }]; center.nowplayinginfo = songinfo; }
i can't figure out why not working :(
progress update's found out far, if reboot iphone, works. every time after won't.
the lock screen , control center both show right track, still can't control app.
not sure if helps @ all, here how received changes (in swift).
override func viewdidload() { super.viewdidload() nsnotificationcenter.defaultcenter().addobserver(self, selector: #selector(self.getnowplayingitem), name: mpmusicplayercontrollernowplayingitemdidchangenotification, object: nil) player.begingeneratingplaybacknotifications() } deinit { player.endgeneratingplaybacknotifications() nsnotificationcenter.defaultcenter().removeobserver(self) }
it necessary (addobserver , begingeneratingplaybacknotifications), (removeobserver , endgeneratingplaybacknotification). not yet familiar remote control events, may want try ending notifications well. hope helps!
Comments
Post a Comment