Music lock Screen Playback

Source: Internet
Author: User

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

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.