如何播放背景iPhone SDK中的音乐
我在一个应用程序工作,我已经在应用程序中设置计时器,之后,我去了后台,当时间结束了,我想在background.but音乐播放音乐?
请参考ios-sdk_background-audio 。 你会非常有帮助的。 祝你好运
使用这个定时器代码来设置定时器
UIBackgroundTaskIdentifier bgTask =0; UIApplication *app = [UIApplication sharedApplication]; bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ [app endBackgroundTask:bgTask]; }]; self.secondsTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFireMethod:) userInfo:nil repeats:NO];
fire方法- (void) timerFireMethod:(NSTimer *) theTimer {}
在此消防方法中使用下面的代码播放媒体文件 –
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"tujhbinjena" ofType:@"mp3"]];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
NSError *error; audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; audioPlayer.delegate=self; audioPlayer.volume=1.0; if (error) { NSLog(@"Error in audioPlayer: %@", [error localizedDescription]); } else { audioPlayer.delegate = self; [audioPlayer prepareToPlay]; [audioPlayer play] }`
[self becomeFirstResponder]; [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; [[AVAudioSession sharedInstance] setActive:YES error:nil];
- (BOOL)canBecomeFirstResponder { return YES; }
在ViewDidUnload中,你需要做以下几行
[[UIApplication sharedApplication] endReceivingRemoteControlEvents]; [self resignFirstResponder];
您需要处理AVAudioPlayerDelegate委托方法,并在后台播放audio,您需要在Info.plist中添加一个参数即“ 所需的背景模式 ”,此键的值为“ 应用程序播放audio ”。 如果有任何疑问,请让我知道。
对于这个function你可以使用这个方法:
-(void)notificationSchedue { NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
// ——— remainingTime NSUserDefaults包含未来date——– //
NSString *datevalueremaining= [[NSUserDefaults standardUserDefaults]valueForKey:@"remainingTime"]; NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease]; [dateFormat setDateFormat:@"dd/MM/yyyy HH:mm:ss"]; NSDate *dateremaining=[dateFormat dateFromString:datevalueremaining]; NSDate *pickerDate = dateremaining; NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit ) fromDate:pickerDate]; NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:pickerDate]; // Set up the fire time NSDateComponents *dateComps = [[NSDateComponents alloc] init]; [dateComps setDay:[dateComponents day]]; [dateComps setMonth:[dateComponents month]]; [dateComps setYear:[dateComponents year]]; [dateComps setHour:[timeComponents hour]]; [dateComps setMinute:[timeComponents minute]]; [dateComps setSecond:[timeComponents second]]; NSDate *itemDate = [calendar dateFromComponents:dateComps]; [dateComps release]; if (localNotif) { [[UIApplication sharedApplication] cancelLocalNotification:localNotif]; [localNotif release]; } localNotif = [[UILocalNotification alloc] init]; if (localNotif == nil) return; localNotif.fireDate = itemDate; localNotif.timeZone = [NSTimeZone defaultTimeZone]; localNotif.alertBody = @"Your Time is Over"; localNotif.alertAction = @"View"; NSString *stringvalue=[[NSUserDefaults standardUserDefaults]valueForKey:@"goodNight"];
// —–音乐应该小于或等于30秒— //
localNotif.soundName=@"s1.mp3"; NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"]; localNotif.userInfo = infoDict; [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; }