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?
-
http://stackoverflow.com/questions/3297571/iphone-playing-audio-playlist-in-the-background check this thread – sachin Aug 07 '12 at 09:55
3 Answers
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.

- 5,020
- 4
- 49
- 75
-
i have tried all things but its not working music is not play in background,if you have any demo code please give me. – ravinder521986 Aug 07 '12 at 12:38
-
-
Yes,i have change all things but not working ,its working fine .but when i went in background music stop........... – ravinder521986 Aug 08 '12 at 06:17
-
Hey, i have added few more lines of code, i have tasted in my sample it is working.So add these updated lines and let me know . – Sanoj Kashyap Aug 09 '12 at 04:39
-
okey ,your code is working fine, but my requirement is different.my requirement is.i have set music in my app with timer,now timer is running,and i went in background,after time completetion ,i want to play music but its not play. – ravinder521986 Aug 09 '12 at 07:01
-
Check whether timer is working or not while you are in the background,If you found my ans working you can make it up.Thanks – Sanoj Kashyap Aug 09 '12 at 07:35
-
Check i have added the few line before creating timer.Use that and let me know. – Sanoj Kashyap Aug 09 '12 at 08:59
-
Nope,Everything i have done here.The previous one is working or not for you? cannot comment more due to limit.you need to explore the rest of the thing. – Sanoj Kashyap Aug 09 '12 at 10:19
-
Check this [Link](http://mobile.tutsplus.com/tutorials/iphone/ios-sdk_background-audio/) – Sanoj Kashyap Aug 09 '12 at 10:43
-
Hey Mani, if it's working you can vote and accept it.That would be really appreciated ! – Sanoj Kashyap Oct 18 '12 at 07:31
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];
}

- 1,180
- 2
- 12
- 30
-
But using this code the alarm will be played just for 30sec. what if I want it to play continously – user2185354 Dec 30 '13 at 05:30