1

I'm trying to play audio file at particular time which i have set.for that I have added audio in "UIBackgroundModes" in info.plist and backgroundtaskidentifier in didEnterBackground.Its working in simulator but not in device.Can anyone help to solve this.

Thanks in Advance...

Ann
  • 243
  • 2
  • 11
  • See this http://stackoverflow.com/questions/7619794/play-music-in-background-in-iphone-using-avaudioplayer/7619816#7619816 – Mehul Mistri Mar 09 '12 at 07:14

2 Answers2

1

If I understand you correctly, you want to play a sound at a particular time, no matter if your app is active and foregrounded at that time? Your only (app review-friendly) option is to schedule a local notification, as in slf's answer. UIBackgroundModes won't help you: the audio background mode is for being allowed to execute code while backgrounded only when you start playing audio when you are foregrounded. Example usages are music players and radio apps.

If you explain your use case more thoroughly, I might have a better answer.

nevyn
  • 7,052
  • 3
  • 32
  • 43
1

You can check out this reference app with streaming and backgrounding of audio.

EDIT:

Example of a local notification w/ attached sound. More detail here.

- (void)applicationDidEnterBackground:(UIApplication *)application {
    NSLog(@"Application entered background state.");
    // bgTask is instance variable
    NSAssert(self->bgTask == UIInvalidBackgroundTask, nil);

    bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [application endBackgroundTask:self->bgTask];
            self->bgTask = UIInvalidBackgroundTask;
        });
    }];

    dispatch_async(dispatch_get_main_queue(), ^{
        while ([application backgroundTimeRemaining] > 1.0) {
            NSString *friend = [self checkForIncomingChat];
            if (friend) {
                UILocalNotification *localNotif = [[UILocalNotification alloc] init];
                if (localNotif) {
                    localNotif.alertBody = [NSString stringWithFormat:
                        NSLocalizedString(@"%@ has a message for you.", nil), friend];
                    localNotif.alertAction = NSLocalizedString(@"Read Message", nil);
                    localNotif.soundName = @"alarmsound.caf";
                    localNotif.applicationIconBadgeNumber = 1;
                    [application presentLocalNotificationNow:localNotif];
                    [localNotif release];
                    friend = nil;
                    break;
                }
            }
        }
        [application endBackgroundTask:self->bgTask];
        self->bgTask = UIInvalidBackgroundTask;
    });

}
slf
  • 22,595
  • 11
  • 77
  • 101
  • it works if we enter the background while audio is playing but i want to start to play audio after entering app into backgrouond.can any one tel me what to do – Ann Mar 03 '12 at 09:42
  • The user initiates audio control manually by double tapping the home button and swiping right, or from the lock screen. Or, you schedule a local notification with a sound clip, not an audio stream – slf Mar 04 '12 at 16:46
  • I want to stop the avaudiostreamer at particular time for eg:it have to play only 45 secs then it have to stop automatically.If I press play button it have to play again from first for 45 secs only.I used timer it works for 1st time for second time its does not.can you help me – Ann Mar 05 '12 at 06:18
  • If I've answered your first question, mark it answered, and ask a new one, and I'll try my best along with everyone else :) – slf Mar 05 '12 at 14:02
  • 1
    A while(1) poll loop is atrocious; doing so when your app is backgrounded is doubly so. Please never ever ever ever use this code in a production app. – nevyn Feb 13 '13 at 13:49
  • @nevyn that code is not mine, it's straight from developer.apple.com, and .... it's not while(1) – slf Feb 13 '13 at 17:08