If you call back, you'll be unlucky.
Make Silent audio.
@interface Appdelegate ()
{
Nsinteger count;
}
@property (Strong, nonatomic) Nstimer *mtimer;
@property (Assign, nonatomic) Uibackgroundtaskidentifier Backiden;
-(void) Applicationdidenterbackground: (uiapplication *) Application {
_mtimer = [Nstimer scheduledtimerwithtimeinterval:1 target:self selector: @selector (countaction) Userinfo:nil repeats: YES];
[[Nsrunloop Currentrunloop] Addtimer:_mtimer formode:nsrunloopcommonmodes];
[Self begintask];
}
Timing
-(void) countaction{
NSLog (@ "%li", count++);
}
Application background
-(void) Begintask
{
NSLog (@ "begin=============");
_backiden = [[UIApplication sharedapplication] beginbackgroundtaskwithexpirationhandler:^{
NSLog (@ "will hang =============");
[Self endback];
}];
}
Logoff background
-(void) Endback
{
NSLog (@ "end=============");
[[UIApplication sharedapplication] endbackgroundtask:_backiden];
_backiden = Uibackgroundtaskinvalid;
}
-(void) Applicationwillenterforeground: (uiapplication *) Application {
[Self endback];
}
#import <AVFoundation/AVFoundation.h>
@property (Strong, nonatomic) Avaudioplayer *mplayer;
@property (Assign, nonatomic) CGFloat mCount;
-(void) Viewdidload {
[Super Viewdidload];
_mcount = 0;
Nstimer *timer = [Nstimer scheduledtimerwithtimeinterval:10 target:self selector: @selector (counttime) Userinfo:nil Repeats:yes];
[[Nsrunloop Currentrunloop] Addtimer:timer formode:nsrunloopcommonmodes];
}
-(void) counttime{
_mcount+=10;
NSLog (@ "%f", _mcount);
if ([[UIApplication sharedapplication] backgroundtimeremaining] < 60.) {//When the remaining time is less than 60 o'clock, open as playing music, and use this fake foreground status again apply backstage
NSLog (@ "Play%@", [Nsthread CurrentThread]);
[Self playmusic];
Application background
[[UIApplication Sharedapplication] beginbackgroundtaskwithexpirationhandler:^{
NSLog (@ "I'm going to hang up");
}];
}
}
-(void) playmusic{
1. The URL path of the audio file, actually developed, with silent music
Nsurl *url=[[nsbundle mainbundle]urlforresource:@ "Wusheng.mp3" withextension:nil];
2. Create player (note: One avaudioplayer can only play one URL)
_mplayer=[[avaudioplayer Alloc]initwithcontentsofurl:url Error:nil];
3. Buffering
[_mplayer Preparetoplay];
_mplayer.numberofloops =-1;
4. Play
[_mplayer play];
}
Play silent audio test on iOS background run is possible