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