我正在开发一个应用程序,我在应用程序中设置了计时器,之后我进入后台,当时间结束时,我想在后台播放音乐。但是音乐没有播放?
3 回答
参考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];
火法——- (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]
}`
[自己成为FirstResponder];[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; [[AVAudioSession sharedInstance] setActive: YES error: nil];
- (BOOL)canBecomeFirstResponder {
return YES;
}
在ViewDidUnload您需要执行以下几行
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
您需要按照处理AVAudioPlayerDelegate委托方法,并且要在后台播放音频,您需要在Info.plist 中添加一个参数,即“所需的背景模式”,此键的值为“应用程序播放音频”。如果有任何疑问,请告诉我。
对于此功能,您可以使用此方法:-
-(void)notificationSchedue
{
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
//---------remainingTime NSUserDefaults 包含未来日期--------//
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];
}