[英]How to play music in background iphone sdk
我正在開發一個應用程序,我已經在應用程序中設置了計時器,之后我進入后台,當時間結束時,我想在后台播放音樂。但音樂不播放?
引用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委托方法並在后台播放音頻,您需要在Info.plist中添加一個參數即“ 必需的背景模式 ”,此鍵的值為“ App播放音頻 ”。 如果有任何疑問,請告訴我。
對於此功能,您可以使用此方法: -
-(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];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.