- The music player I use is my own avplayer.
- Import header File #import <MediaPlayer/MediaPlayer.h>
- Remote control event reception and processing
-(void) Viewwillappear: (BOOL) animated
{
[Super viewwillappear:animated];
[[UIApplication sharedapplication] beginreceivingremotecontrolevents];
[Self becomefirstresponder];
}
-(void) Viewdiddisappear: (BOOL) animated{
[[UIApplication sharedapplication] endreceivingremotecontrolevents];
[Self resignfirstresponder];
}
-(void) Remotecontrolreceivedwithevent: (Uievent *) event
{
if (Event.type = = Uieventtyperemotecontrol) {
Switch (event.subtype) {
Case Uieventsubtyperemotecontrolplay:
[Self play]; Play
Break
Case Uieventsubtyperemotecontrolpause:
[Self pause];//paused
Break
Case Uieventsubtyperemotecontrolprevioustrack:
[Self forwarditem]; Play the previous Song button
Break
Case Uieventsubtyperemotecontrolnexttrack:
[Self nextitem]; Play next Song button
Break
Default
Break
}
}
}
- Passing information to the lock screen state-(void) Configplayinginfo This method is called when playing songs and switching songs
{
if (nsclassfromstring (@ "Mpnowplayinginfocenter")) {
if ((Lastplayitem! = Self.player.currentItem) &&!isrepeat) {
Lastplayitem = Self.player.currentItem;
Nsmutabledictionary *dict = [[Nsmutabledictionary alloc]init];
[Dict setObject:self.titleLabel.text forkey:mpmediaitempropertytitle];//song name settings
[Dict setObject:self.artistLabel.text forkey:mpmediaitempropertyartist];//singer name settings
[Dict Setobject:[[mpmediaitemartwork alloc] initWithImage:self.artwork.image] forkey:mpmediaitempropertyartwork];/ /album Picture settings
[Dict setobject:[nsnumber Numberwithdouble:cmtimegetseconds (self.player.currentItem.currentTime)] Forkey: Mpnowplayinginfopropertyelapsedplaybacktime]; Music is currently playing time
[Dict Setobject:[nsnumber numberwithfloat:1.0] The speed of the forkey:mpnowplayinginfopropertyplaybackrate];//progress cursor (this with Own playback rate adjustment, I default is the original speed play)
[Dict setobject:[nsnumber Numberwithdouble:cmtimegetseconds (self.player.currentItem.duration)] Forkey: mpmediaitempropertyplaybackduration];//Song Total time setting
[[Mpnowplayinginfocenter Defaultcenter] setnowplayinginfo:dict];
}
}
}
- A few points to note is that each time you pause you need to save the current music playback progress and the speed of the progress cursor under the lock screen is set to a number close to 0 (0.00001) so that the next time you resume playback, the progress cursor position under the lock screen will be OK. The following code: nsmutabledictionary *dict = [Nsmutabledictionary dictionarywithdictionary:[[mpnowplayinginfocenter Defaultcenter] Nowplayinginfo];
[Dict setobject:[nsnumber Numberwithdouble:cmtimegetseconds (cmtimemakewithseconds (mslider.value/timess) *timess, Self.player.currentItem.currentTime.timescale)] forkey:mpnowplayinginfopropertyelapsedplaybacktime];
[[Mpnowplayinginfocenter Defaultcenter] setnowplayinginfo:dict];
Swift lock screen playback, music progress update, album, singer name Display