如何在背景iphone sdk播放音乐

时间:2012-08-07 09:49:40

标签: iphone objective-c ios

我正在开发一个应用程序,我已经在应用程序中设置了计时器,之后我进入了后台,当时间结束时我想在后台播放音乐。但是音乐不能播放?

3 个答案:

答案 0 :(得分:3)

引用ios-sdk_background-audio。你会非常乐于助人。祝你好运

答案 1 :(得分:3)

使用此定时器代码设置定时器

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]
}`  
  

[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 中添加一个参数,即“必需后台模式“此键的值为”应用播放音频“。如果有任何疑问,请告诉我。

答案 2 :(得分:1)

对于此功能,您可以使用此方法: -

 -(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];

 }