Swift lock screen playback, music progress update, album, singer name Display

Source: Internet
Author: User

    1. The music player I use is my own avplayer.
    2. Import header File #import <MediaPlayer/MediaPlayer.h>
    3. 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
      }
      }
      }
    4. 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];

      }
      }
      }
    5. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.