4GetFullOf
4GetFullOf

Reputation: 1738

Can't pause audio during applicationDidEnterBackground

I can't change the values or alter any variables or objects declared. Can anyone see what's wrong?

In my app delegate method.

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

    MainPage *mp = [[MainPage alloc]init];

    mp.newTime = [[NSDate date] timeIntervalSinceDate:mp.date];
    [mp.timer invalidate];
    mp.switchT = 1;
    mp.playBarButton.enabled = YES;
    mp.pauseBarButton.enabled = NO;
    mp.stopBarButton.enabled = YES;
   [mp.avPlayer pause];

}

None of these actions above are executed. In my mainpage.h file I have the followed declared like this:

@property (nonatomic, retain) AVAudioPlayer *avPlayer;
@property (strong, retain) NSTimer *timer;
@property (strong, retain) NSDate *date;
@property (nonatomic) NSTimeInterval newTime;
@property (nonatomic) NSInteger switchT;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *playBarButton;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *pauseBarButton;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *stopBarButton;

Of course in my implementation file these are synthesized and all works well with my code within mainpage.m just not in my appDelegate file. Can anyone see the problem? Note this is a fully functioning app everything works well just can't execute the following code in my applicationDidEnterBackground method and I'm assuming it's the way my properties are made? Also no errors with my code written in the app delegate.

Upvotes: 1

Views: 317

Answers (2)

4GetFullOf
4GetFullOf

Reputation: 1738

NSNotificationCenter defaultCenter] addObserver:self selector:@selector(goBackground) name:UIApplicationDidEnterBackgroundNotification object:nil]; // and later
 - (void) goBackground { [timer invalidate], timer = nil; }

Upvotes: 2

Phillip Mills
Phillip Mills

Reputation: 31016

This MainPage *mp = [[MainPage alloc]init]; create a new MainPage object. Changing values in one object is not going to affect the display of a different MainPage object.

Either give your app delegate a reference to the object you're actually displaying or put a handler for a background notification inside MainPage so that it can do its own work.

Upvotes: 2

Related Questions