# # # Music background Play*1when the program enters the background, open the background task " "- (void) Applicationdidenterbackground: (UIApplication *) { //turn on background tasks[Application Beginbackgroundtaskwithexpirationhandler:nil]; } ```*2. On the project's targets page, set Capabilities,backgroundmodes Select the first item, ' Audio, AirPlay and pictureinchPicture '*3. Set audiosession session " "-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions {//1. Get avaudiosession sessionAvaudiosession *session =[Avaudiosession sharedinstance]; //2. Setting the type[Session Setcategory:avaudiosessioncategoryplayback Error:nil]; //3. Activate[Session Setactive:yes Error:nil]; returnYES; } ' # # # Display lock screen information*1when starting playback, unlock the lock screen message and extract a method ' [self updatelockinfo]; ```*2. Import ' mediaplayer/MediaPlayer.h ' frame, setting information in playback InfoCenter- (void) Updatelockinfo {//Get playback DashboardMpnowplayinginfocenter *center =[Mpnowplayinginfocenter Defaultcenter]; //Create an information dictionaryNsmutabledictionary *infos =[Nsmutabledictionary dictionary]; //Setting information ContentInfos[mpmediaitempropertyalbumtitle] =Self.playingMusic.name; Infos[mpmediaitempropertyalbumartist]=Self.playingMusic.singer; Infos[mpmediaitempropertyartwork]=[[Mpmediaitemartwork alloc] initwithimage:[uiimage ImageNamed:self.playingMusic.icon]]; Infos[mpmediaitempropertyplaybackduration]=@ (self.player.duration); Infos[mpnowplayinginfopropertyelapsedplaybacktime]=@ (self.player.currentTime); //set the playback center based on a dictionary[center Setnowplayinginfo:infos]; //Turn on remote events[[UIApplication sharedapplication] beginreceivingremotecontrolevents]; //make the current controller a first responder[self becomefirstresponder]; } ```*3. The controller cannot be the first responder by default, and the following methods need to be implemented-(BOOL) Canbecomefirstresponder {returnYES; } ```*4. Listen for remote events '- (void) Remotecontrolreceivewithevent: (Uievent *)Event { Switch(Event. Subtype) { //When you click the Play or pause button CaseUieventsubtyperemotecontrolplay: Caseuieventsubtyperemotecontrolpause: [Self playorpausebuttonclick]; Break; //when I click on the Next button Caseuieventsubtyperemotecontrolnexttrack: [Self nextbuttonclick]; Break; //when I click on the previous button Caseuieventsubtyperemotecontrolprevioustrack: [Self previousbuttonclick]; Break; default: Break; } ```
iOS background playback