Background play lock screen for iOS audio

Source: Internet
Author: User

Initializing Audiosession and basic configuration

The audio player uses the Avplayer, when the program starts the need to configure the audiosession,audiosession is responsible for the application of audio settings, such as support does not support the background, interrupt and so on, this step is very important, For example, after initializing Avplayer in Viewdidload, call the following function:

/** Set Audio Session *///This way background, can continuously play non-network request song, encounter network request song on the scrap, need to apply for task background

-(void) setaudiosession{

Avaudiosession *audiosession=[avaudiosession Sharedinstance];

[Audiosession Setcategory:avaudiosessioncategoryplayback Error:nil];

[Audiosession Setactive:yes Error:nil];

}

In addition to the initialization of the code, an important step is to set the info-plist so that the app supports audio background playback

The introduction of libraries includes:

Audiotoolbox.framework

Mediaplayer.framework

Coremedia.framework

Avfoundation.framework

Remote control

Add these two functions to the viewcontroller of the playback view:

-(void) Viewdidappear: (BOOL) Animated {

[Super viewdidappear:animated];

[[UIApplication sharedapplication] beginreceivingremotecontrolevents];

[Self becomefirstresponder];

}

-(void) Viewwilldisappear: (BOOL) Animated {

[Super viewwilldisappear:animated];

[[UIApplication sharedapplication] endreceivingremotecontrolevents];

[Self resignfirstresponder];

}

Of course, you can also put in the same way into the DELEGATE.M inside the backstage and back to the foreground function, otherwise, the above code only allows the current view of the case into the background can be remote control

In Appdelegate to apply for a background task to process

-(void) Applicationdidenterbackground: (uiapplication *) Application {

[Application beginreceivingremotecontrolevents];
}

When adding remote control code:

-(void) Remotecontrolreceivedwithevent: (uievent *) event{

If it is a remote control event handle it correctly

if (Event.type = = Uieventtyperemotecontrol) {

if (Event.subtype = = Uieventsubtyperemotecontrolplay) {

[Self PlayBarSelector:self.mPlayButton];

}if (Event.subtype = = Uieventsubtyperemotecontrolpause) {

[Self PlayBarSelector:self.mPlayButton];

} else if (Event.subtype = = Uieventsubtyperemotecontrolnexttrack) {

[Self PlayBarSelector:self.mNextButton];

[Self confignowplayinginfocenter];

}else if (Event.subtype = = Uieventsubtyperemotecontrolprevioustrack) {

[Self PlayBarSelector:self.mUpwardButton];

[Self confignowplayinginfocenter];

}

}

}

Finally switch the previous and Next top update lock screen information, re-tune the Confignowplayinginfocenter method

-(void) Confignowplayinginfocenter {

if (nsclassfromstring (@ "Mpnowplayinginfocenter")) {

1. Playback Information Center

Mpnowplayinginfocenter *center = [Mpnowplayinginfocenter defaultcenter];

2. Initializing playback information

Nsmutabledictionary *info = [Nsmutabledictionary dictionary];

Album Name

Info[mpmediaitempropertyalbumtitle] = Self.operatorObject.mExerciseText;

Singer

Info[mpmediaitempropertyartist] = @ "IELTS Listening Group";

Song name

Info[mpmediaitempropertytitle] = [NSString stringwithformat:@ "%@-%@", Self.operatorObject.mTextName, [ Self.operatorObject.mTitle Substringtoindex:9]];

Set up a picture

Info[mpmediaitempropertyartwork] = [[Mpmediaitemartwork alloc] initwithimage:[uiimage imagenamed:@ "Default"];

Set duration (total time of song)

Info[mpmediaitempropertyplaybackduration] = @ (Self.maudioplayerlong);

Set Current playback progress

Info[mpnowplayinginfopropertyelapsedplaybacktime] = @ (self.mplayercurrenttime);

3. Toggle Playback Information

Center.nowplayinginfo = info;

}

}

Background play lock screen for iOS audio

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.