Frage

I have a notification setup to alert my VC when the app is going to resign the active state:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(pauseGame)
                                             name:UIApplicationWillResignActiveNotification
                                           object:[UIApplication sharedApplication]];

As you can see it calls a method called pauseGame.
That method, amongst other things, contains:

[audio pauseAudio];

I use this same method within the app to pause and everything works as it should.

This method is properly being called when the app is being sent to the background, however, when the app is resumed the audio continues to play. The other pause items are working on restore so the method is completing.

How can I get the audio to pause properly when entering the background?

Update: People have been mentioning the ApplicationDidEnterBackGroundNotification. I tried that but it works the same. It should be noted that Apple in their own comments in the AppDelegate recommend the method I originally used. See below for their pre-placed comment within the delegate.

    - (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

Update 2: Using WillResignActive works correctly for pausing when a call comes in for example. With that event the audio stops properly. So it seems to be just about the app going to the background when the home button is pressed. The background notification isn't called for incoming calls so obviously a call and home press result in different states.

War es hilfreich?

Lösung 4

I have found stopping the audio versus pausing it takes care of the issue. It is interesting to note that stop does not start the audio over when resumed but rather will start where it left off. In this way stop works the same as pause and satisfies my needs.

Andere Tipps

    - (void)applicationDidEnterBackground:(UIApplication *)application
 {
   /*
   Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
 If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
   */
    NSLog(@"Application moving to background");
  // Here Call your notification for pause  audio
}

https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html

Just try this way...

 -(void)applicationDidEnterBackground:(UIApplication *)application
     {

    bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{

            [[UIApplication sharedApplication] endBackgroundTask:bgTask];
        }];

    }

Also declare bgTask on Appdelegate.h

UIBackgroundTaskIdentifier bgTask;

Here you just change the NSNotifcation Centre as

[[NSNotificationCenter defaultCenter] addObserver: self
                                                 selector: @selector(pauseGame)
                                                     name: UIApplicationDidEnterBackgroundNotification
                                                   object: nil];

. Hope this will help you....

Write this in your viewDidLoad:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pausePlayer) name:UIApplicationDidEnterBackgroundNotification object:nil];

and add a new method:

-(void)pausePlayer{
    [audio pauseAudio];
}

There are many different notifications, just check the UIApplication.h

I know this is an ancient question, but I just found this solution to the same problem (which is still around in 2018 with iOS 11 and I assume iOS 12):

Register for AVAudioSessionInterruptionNotification:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(avAudioSessionInterruptionNotification:) name:AVAudioSessionInterruptionNotification object:nil];

When your application gets resumed you'll get this notification. When you resume the app, you'll be told that the interruption began then immediately be told that it ended. If your audio is playing, you will pause it in the "began" handler. Then "ended" message will not have the "resume" value, so you should stop your audio. Here's my code:

- (void) avAudioSessionInterruptionNotification:(NSNotification *)notification
    {
    AVAudioSessionInterruptionType type = [notification.userInfo[AVAudioSessionInterruptionTypeKey] intValue];
    if ( type == AVAudioSessionInterruptionTypeBegan )
        {
        // If your audio is active, pause it here.
        }
    else if ( type == AVAudioSessionInterruptionTypeEnded )
        {
        AVAudioSessionInterruptionOptions optionsValue = [notification.userInfo[AVAudioSessionInterruptionOptionKey] intValue];
        if ( optionsValue == AVAudioSessionInterruptionOptionShouldResume )
            {
            // Resume your audio here
            }
        else
            {
            // Stop your audio here
            }
        }
    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top