原文地址:blog.csdn.net/ios_dashen/article/details/50352204
準備工作:
1.導(dǎo)入`AVFoundation.framework`

2.導(dǎo)入一個無聲音樂文件 (.mp3)

3.在info.plist里面請求后臺播放音樂的權(quán)限

4.上代碼
#import "AppDelegate.h"
#import?
@interfaceAppDelegate?()
@property(strong,nonatomic)NSString*startTime;
@end
@implementationAppDelegate
-?(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions?{
//?Override?point?for?customization?after?application?launch.
AVAudioSession*session?=?[AVAudioSessionsharedInstance];
[sessionsetActive:YESerror:nil];
[sessionsetCategory:AVAudioSessionCategoryPlaybackerror:nil];
//讓?app?支持接受遠程控制事件
[[UIApplicationsharedApplication]beginReceivingRemoteControlEvents];
//播放背景音樂
NSString*musicPath=[[NSBundlemainBundle]pathForResource:@"wusheng"ofType:@"mp3"];
NSURL*url=[[NSURLalloc]initFileURLWithPath:musicPath];
//創(chuàng)建播放器
AVAudioPlayer*audioPlayer=[[AVAudioPlayeralloc]initWithContentsOfURL:urlerror:nil];
[audioPlayerprepareToPlay];
//無限循環(huán)播放
audioPlayer.numberOfLoops=-1;
[audioPlayerplay];
[NSTimerscheduledTimerWithTimeInterval:1.ftarget:selfselector:@selector(printCurrentTime:)userInfo:nilrepeats:YES];
returnYES;
}
-(void)printCurrentTime:(id)sender{
NSLog(@"當(dāng)前的時間是---%@---",[selfgetCurrentTime]);
}
-(NSString*)getCurrentTime{
NSDateFormatter*dateFormatter=[[NSDateFormatteralloc]init];
[dateFormattersetDateFormat:@"yyyy-MM-DD?HH:mm:ss"];
NSString*dateTime=[dateFormatterstringFromDate:[NSDatedate]];
self.startTime=dateTime;
returnself.startTime;
}
-?(void)applicationWillResignActive:(UIApplication*)application?{
//?Sent?when?the?application?is?about?to?move?from?active?to?inactive?state.?This?can?occur?for?certain?types?of?temporary?interruptions?(such?as?an?incoming?phone?call?or?SMS?message)?or?when?the?user?quits?the?application?and?it?begins?the?transition?to?the?background?state.
//?Use?this?method?to?pause?ongoing?tasks,?disable?timers,?and?throttle?down?OpenGL?ES?frame?rates.?Games?should?use?this?method?to?pause?the?game.
}
-?(void)applicationDidEnterBackground:(UIApplication*)application?{
//?Use?this?method?to?release?shared?resources,?save?user?data,?invalidate?timers,?and?store?enough?application?state?information?to?restore?your?application?to?its?current?state?in?case?it?is?terminated?later.
//?If?your?application?supports?background?execution,?this?method?is?called?instead?of?applicationWillTerminate:?when?the?user?quits.
}
-?(void)applicationWillEnterForeground:(UIApplication*)application?{
//?Called?as?part?of?the?transition?from?the?background?to?the?inactive?state;?here?you?can?undo?many?of?the?changes?made?on?entering?the?background.
}
-?(void)applicationDidBecomeActive:(UIApplication*)application?{
//?Restart?any?tasks?that?were?paused?(or?not?yet?started)?while?the?application?was?inactive.?If?the?application?was?previously?in?the?background,?optionally?refresh?the?user?interface.
}
-?(void)applicationWillTerminate:(UIApplication*)application?{
//?Called?when?the?application?is?about?to?terminate.?Save?data?if?appropriate.?See?also?applicationDidEnterBackground:.
}
@end
只要不占用太多內(nèi)存,活著用戶手動的kill程序,看打印的log知道這個app可以長時間的在后臺運行