4

I am working on an app,i have set timer in app, after that i went in background and when time is over i want to play music in background.but music is not play?

ravinder521986
  • 722
  • 9
  • 17
  • http://stackoverflow.com/questions/3297571/iphone-playing-audio-playlist-in-the-background check this thread – sachin Aug 07 '12 at 09:55

3 Answers3

3

refer a ios-sdk_background-audio. You'll be very helpful. Good luck

bitmapdata.com
  • 9,572
  • 5
  • 35
  • 43
3

Use this Timer Code for set the Timer

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 Method-- (void) timerFireMethod:(NSTimer *) theTimer {}

In This fire Method use Below Code for Playing media file-

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;
}

and in ViewDidUnload you need to do these following lines

[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];

you need to handle the AVAudioPlayerDelegate delegate methods as per and for playing audio in background you need to add one Parameter in Info.plist That is "Required background modes" and value for this key is "App plays audio". Please let me know if any doubt.

Sanoj Kashyap
  • 5,020
  • 4
  • 49
  • 75
1

for this function you can use this method:--

 -(void)notificationSchedue

   {

  NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

//---------remainingTime NSUserDefaults contain the future 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"];

//-----music should be less then or equal to 30 sec---//

   localNotif.soundName=@"s1.mp3";

    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];

 localNotif.userInfo = infoDict;

 [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

 }
Jaspreet Singh
  • 1,180
  • 2
  • 12
  • 30