准備工作:
1.導入AVFoundation.framework
2.導入一個無聲音樂文件 (.mp3)
3.在info.plist里面請求后台播放音樂的權限
4.上代碼
- #import "AppDelegate.h"
- #import <AVFoundation/AVFoundation.h>
- @interface AppDelegate ()
- @property (strong, nonatomic) NSString *startTime;
- @end
- @implementation AppDelegate
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- // Override point for customization after application launch.
- AVAudioSession *session = [AVAudioSession sharedInstance];
- [session setActive:YES error:nil];
- [session setCategory:AVAudioSessionCategoryPlayback error:nil];
- //讓 app 支持接受遠程控制事件
- [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
- //播放背景音樂
- NSString *musicPath=[[NSBundle mainBundle] pathForResource:@"wusheng" ofType:@"mp3"];
- NSURL *url=[[NSURL alloc]initFileURLWithPath:musicPath];
- //創建播放器
- AVAudioPlayer *audioPlayer=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
- [audioPlayer prepareToPlay];
- //無限循環播放
- audioPlayer.numberOfLoops=-1;
- [audioPlayer play];
- [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(printCurrentTime:) userInfo:nil repeats:YES];
- return YES;
- }
- -(void)printCurrentTime:(id)sender{
- NSLog(@"當前的時間是---%@---",[self getCurrentTime]);
- }
- -(NSString *)getCurrentTime{
- NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
- [dateFormatter setDateFormat:@"yyyy-MM-DD HH:mm:ss"];
- NSString *dateTime=[dateFormatter stringFromDate:[NSDate date]];
- self.startTime=dateTime;
- return self.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
只要不占用太多內存,活着用戶手動的kill程序,看打印的log知道這個app可以長時間的在后台運行