1, first to music support background play
(1) Add in Info.plist
Required Background modes
Item 0:app plays audio or streams audio/video using AirPlay (2) Set Avaudiosession:
Avaudiosession *session = [avaudiosession sharedinstance]; [Session Setactive:yes Error:nil]; [Session Setcategory:avaudiosessioncategoryplayback Error:nil];
(3) Registering events in the background:
[[UIApplication sharedapplication] beginreceivingremotecontrolevents];
2. Lock screen Setting
Determine lock screen events for event handling
-(void) Remotecontrolreceivedwithevent: (Uievent *)Event{ Switch(Event. Subtype) { Caseuieventsubtyperemotecontrolpause: [Self.currentaudioplayer pause]; Break; CaseUieventsubtyperemotecontrolnexttrack:if(Self.rotatecell) {[Self.rotatecell stoprotate]; Nsindexpath*indexpath =[Self.tableview Indexpathforselectedrow]; Swtmusic*music =Self.musicarray[indexpath.item]; [Swtmusictool StopMusic:music.filename]; intindex = Indexpath.item +1; if(Index >=self.musicArray.count) {index=0; } Nsindexpath*newindexpath = [Nsindexpath indexpathforitem:index insection:0]; [Self.tableview Selectrowatindexpath:newindexpath Animated:yes scrollposition:uitableviewscrollpositiontop]; Swtmusiccell*cell =[Self.tableview Cellforrowatindexpath:newindexpath]; Self.rotatecell=cell; [Cell startrotate]; Swtmusic*nextmusic =Self.musicarray[index]; [Self showinfoinlockedscreen:nextmusic]; Self.currentaudioplayer=[Swtmusictool PlayMusic:nextMusic.filename]; Self.currentaudioplayer.Delegate=Self ; } Break; CaseUieventsubtyperemotecontrolprevioustrack:if(Self.rotatecell) {[Self.rotatecell stoprotate]; Nsindexpath*indexpath =[Self.tableview Indexpathforselectedrow]; intindex = Indexpath.item-1; if(Index <0) {Index= self.musicarray.count-1; } Nsindexpath*newindexpath = [Nsindexpath indexpathforitem:index insection:0]; Swtmusic*music =Self.musicarray[indexpath.item]; [Swtmusictool StopMusic:music.filename]; [Self.tableview Selectrowatindexpath:newindexpath Animated:yes scrollposition:uitableviewscrollpositiontop]; Swtmusiccell*cell =[Self.tableview Cellforrowatindexpath:newindexpath]; Self.rotatecell=cell; [Cell startrotate]; Swtmusic*nextmusic =Self.musicarray[index]; [Self showinfoinlockedscreen:nextmusic]; Self.currentaudioplayer=[Swtmusictool PlayMusic:nextMusic.filename]; Self.currentaudioplayer.Delegate=Self ; } Break; CaseUieventsubtyperemotecontrolplay: [Self.currentaudioplayer play]; Break; default: Break; }}
4. Lock screen Information
-(void) Showinfoinlockedscreen: (swtmusic*) music{//this way, the information cannot be displayed correctly on the lock screen//nsmutabledictionary *info = [nsmutabledictionary dictionary];//Info[mpmediaitempropertytitle] = music.name;//info[mpmediaitempropertyartist] = Music.singer;//info[mpmediaitempropertyartwork] = [[Mpmediaitemartwork alloc] Initwithimage:[uiimage imagenamed:music.icon]];< /c5>// //[[Mpnowplayinginfocenter Defaultcenter] setnowplayinginfo:info];nsmutabledictionary*songinfo =[[Nsmutabledictionary alloc] init]; Mpmediaitemartwork*albumart =[ [Mpmediaitemartwork alloc] Initwithimage: [UIImage ImageNamed:music.icon]]; //set the playback progress bar DoubleCurrentTime =Self.currentAudioPlayer.currentTime; DoubleDuration =self.currentAudioPlayer.duration; [Songinfo SetObject:music.name Forkey:mpmediaitempropertytitle]; [Songinfo SetObject:music.singer forkey:mpmediaitempropertyartist]; [Songinfo SetObject:music.singer Forkey:mpmediaitempropertyalbumtitle]; [Songinfo Setobject:albumart forkey:mpmediaitempropertyartwork]; [Songinfo setobject:@ (CurrentTime) forkey:mpnowplayinginfopropertyelapsedplaybacktime]; [Songinfo setobject:@ (Duration) forkey:mpmediaitempropertyplaybackduration]; [[Mpnowplayinginfocenter Defaultcenter] setnowplayinginfo:songinfo];}
Music lock Screen Playback