iOS background playback

Source: Internet
Author: User

# # # 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

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.