Stop AVAudioPlayer music when the app goes to background - ios

I'm developing a little game and I'm using AVAudioPlayer in order to play the game's background music. The problem is that if the user exits the app without closing it in the app drawer (so it is in the background) the music will not stop. How can I change this? Thanks for your help!

In the ViewController where you handle your AVAudioPlayer add this in your viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(appWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(appWillTerminate:) name:UIApplicationWillTerminateNotification object:nil];
Then you add those 2 methods :
-(void) appWillResignActive:(NSNotification*)note{
NSLog(#"App is going to the background");
// This is where you stop the music
}
-(void) appWillTerminate:(NSNotification*)note{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillTerminateNotification object:nil];
NSLog(#"App will terminate");
}

The quick answer here is that you want to configure the AVAudioSession to use the category AVAudioSessionCategorySoloAmbient.
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategorySoloAmbient error:nil];
This before you activate the session of course.

Related

How to receive Notification in UIWebView Player for full screen mode in iOS 8?

I want to do some stuff after video playing in full screen mode in UIWebView.
So, I want message from UIWebView for entering in full screen & exit from full screen.
In iOS 7 I am getting notification by below stuff:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(VideoEnterFullScreenHere:)
name:#"UIMoviePlayerControllerDidEnterFullscreenNotification"
object:self.view.window];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(VideoExitFullScreenHere:)
name:#"UIMoviePlayerControllerDidExitFullscreenNotification"
object:self.view.window];
But in iOS 8, it is not working properly.
Below stuff is worked for me.
I hope it will help others!
In your AppDelegate.m class,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(windowBecameHidden:) name:UIWindowDidBecomeVisibleNotification object:nil];
return YES;
}
And receive it by this,
- (void)windowBecameHidden:(NSNotification *)notification {
UIWindow *window = notification.object;
if (window != self.window) {
NSLog(#"Online video on full screen.");
}
}
Thank You!
Use AVPlayer Notification instead of UIMoviePlayer Notification.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playerItemInitiated:)
name:#"AVPlayerItemBecameCurrentNotification"
object:nil];

NSNotificationCenter calling two times

Below is what I have.
MainViewController.m
- (IBAction)sideMenuAction:(id)sender {
NSLog(#"login==sideMenuAction");
[[NSNotificationCenter defaultCenter] postNotificationName:#"ShowMySideMenuNotification" object:self];
}
NotificationListener.m
-(void)viewDidLoad {
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"ShowMySideMenuNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(adjustShowMenu) name:#"ShowMySideMenuNotification" object:nil];
}
-(void) adjustShowMenu {
NSLog(#"notification adjustShowMenu=");
}
Now when I click side menu button in MainViewController, what I was expecting is call adjustShowMenu from NotificationListener once, however it is called twice.
Below is the NSLog for the same.
2015-01-20 12:27:30.798 abc[699:169314] login==sideMenuAction
2015-01-20 12:27:30.798 abc[699:169314] notification adjustShowMenu=
2015-01-20 12:27:30.799 abc[699:169314] notification adjustShowMenu=
What I was expecting is
2015-01-20 12:27:30.798 abc[699:169314] login==sideMenuAction
2015-01-20 12:27:30.798 abc[699:169314] notification adjustShowMenu=
Any idea what is going wrong?
Note: I also tried in viewDidAppear instead of viewDidLoad, but its giving same result.
When I searched online, many answers asked to removeObserver. I did same, but still twice notification is getting called.
As per answer here, I make changes as below and its working fine now.
-(void) viewWillAppear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(adjustShowMenu) name:#"ShowMySideMenuNotification" object:nil];
}
-(void) viewWillDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"ShowMySideMenuNotification" object:nil];
}

UIWebView Embed video callbacks are not working in iOS8?

In my application I am loading some webpages embedded with Photos and Videos. Also I am using the following notifications to manage the player,
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(embeddedVideoStarted:) name:#"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(embeddedVideoEnded:) name:#"UIMoviePlayerControllerWillExitFullscreenNotification" object:nil];
This is working fine in iOS7, but in iOS8 its not working. Any workarounds? Thanks in advance.
This is one option, I have found.. The problem is that is not Will is DID become hidden..
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(embeddedVideoStarted:)
name:UIWindowDidBecomeVisibleNotification
object:self.view.window];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(embeddedVideoEnded:)
name:UIWindowDidBecomeHiddenNotification
object:self.view.window];
If, I find a fix for the second notification I will posted it.. :)

Using instance methods in the app delegate

I wanted to run a instance method from my BPGameController class called "pauseGame" in my app delegate when my application enters background mode and when it resigns, and a instance method called "resumeGame" when the application becomes active again, I've tried a few different things but none have worked for me so far
In BPGameController's viewDidLoad method sign up for the notifications as follows:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(pauseGame)
name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(resumeGame)
name:UIApplicationDidBecomeActiveNotification object:nil];
When you are done remove yourself from observing.
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];

multiple notifications in MPMoviePlayerController

Is it possible to have two different playback notifications in a MPMoviePlayerController?
I want to use these:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(MPMoviePlayerPlaybackDidChange:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:self.moviePlayer];
i did manage to get them working but when i use them together only MPMoviePlayerPlaybackStateDidChangeNotification is called.
Please advise.

Resources