I am developing the Game iPhone application, it has Music player and and few animation. The game is interrupted by a text message or call it is terminated. It should pause the game until you click cancel. But While sound plays if i receive the call or message, audio player is pause, after accept/decline call, it doesn't play continue. how to manage this? actualy the
Have a look at the UIApplicationDelegate reference. There you'll see the various methods that are called as events beyond your control occur (such as a phone call coming in, etc.)
In your case I believe you want to override the applicationWillResignActive and applicationDidBecomeActive methods to handle pausing the game, sounds, etc. and restoring them.
UPDATE: I found this blog post very helpful in understanding all the multitasking delegates: http://www.cocoanetics.com/2010/07/understanding-ios-4-backgrounding-and-delegate-messaging/
Related
I have a quiz application in which the duration of each question is 20 sec. It works fine, but in case, if a user gets a call while playing, I want to pause the application & set it in background.
Does anyone have any idea?
It depends on the implementation of your app but, broadly, your application should stop any running timers, pause the game, pause any sound playback, etc. in the applicationWillResignActive: method.
When the call is finished, your app is invoked again and the applicationDidBecomeActive: method is called. You should resume timers, sound playback, etc. from that method. If it's a game, you should not resume it automatically; keep it paused and wait for the user to resume it manually to comply with Apple's UX guidelines.
When a call comes in, your application is suspended.
And you'll know this is happening because your "UIApplicationDelegate" will get a message of "applicationWillResignActive:". When it comes back, you can start up where you left off or you can bring up a new question or whatever you want to do, it's up to how you decide to implement your quiz app.
When user gets a phone call in iphone application automatically goes into applicationDidEnterBackground state.for example if you are working on radio app then radio automatically get off when user get any call after call ended application comes into below method(appDelegate.m)
(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.
}
I'm using Cocos2D to write a game for iOS. I have implemented a pause feature that allows the user to a pause menu on request. I also implement a complete pause (unschedule main game loop) when receiving a applicationWillResignActive. On applicationWillEnterForeground and applicationDidBecomeActive I resume the main game loop, but then throw the player into the pause menu, forcing them to resume when ready. Everything seems to be working well except for one situation. When receiving a calendar event alarm (which I assume is a UIAlert), the game does not throw up a pause menu or pause the game.
My three questions: must I include both willEnterForeground and didBecomeActive, or is one good enough for my purposes? What is the call I must schedule for to receive UIAlerts (if that is in fact what triggers my error in the above case) and how should I handle them? Is there some way to simulate a UIAlert so I can test/debug?
Much appreciated.
Edit -
The problem was in my overall handling of said events. As -clearly- stated in apple's documentation, use willResignActive to pause the game (i.e. throw up a pause menu and stop the game loop), and didEnterForeground to resume the game loop, but keep the pause menu up. By handling the events this way, there was no need to schedule to receive UIAlerts or Local/Remote Notifications.
When ever you will get a calendar event you will get a call of applicationWillResignActive: , this is a place to handle cocos2D pause or any-other thing you want to do at calendar event reception. If calender event ignored applicationDidBecomeActive will be called. If not ignored then you will have to resume from same you are doing.
I have an app, that will keep track of everything the user do in the iPod app. To do this, I added few observers to NSNotificationCenter, like MPMusicPlayerControllerNowPlayingItemDidChangeNotification. But my problem is, I only get those notifications when my app is in the foreground, if its in the background, the system add the notification to a queue, and then the next time my app becomes active it delivers it to me. I have no interest in this queue, since I want to receive real-time notifications.
Is there any way for me to get those notifications even if my app is in suspended state? I want to run just 3 lines of code everytime I get this NowPlayingItemDidChange notifications for example.
Here is where I add the observer.
MPMusicPlayerController *iPodMediaPlayer = [MPMusicPlayerController iPodMusicPlayer];
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver: self selector: #selector(handle_NowPlayingItemChanged:) name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification
object:iPodMediaPlayer];
[iPodMediaPlayer beginGeneratingPlaybackNotifications];
Also, if I add another kind of object to the observer instead of iPodMediaPlayer, the observer won't call the method.
Thanks a lot,
Abras
iOS applications are suspended when they are not in the foreground. There are three exceptions to this rule. You can have code execute in the background if your application is
a) Playing audio. This means the application itself is actually generating audio. My understanding is that the MPMediaPlayerController iPodMusicPlayer object only controls the playback of the external iPod process, rather than playing audio from the app itself. Perhaps you could have some success if you called applicationMusicPlayer instead of iPodMusicPlayer and set the appropriate background flags in your applications Info.plist. This seems like the most legitimate way to get your application to work, but you wouldn't be able to control iPod playback from the iPod app, only your app and the system audio controls.
b) Get your app to monitor the location. If the app is using the GPS it can continue to execute in the background. Downside to this is that the GPS will drain battery, and users might be creeped out that you're requesting their location.
c) Ask UIApplication for extra time. If you use UIApplication's beginBackgroundTask method, your application will continue to run for a finite amount of time in the background. If your users are going to come into your application once every ten minutes or so, this could work as well.
Hope that helps.
Multitasking in iOS is currently a very restricted one. You may start a background task using beginBackgroundTaskWithExpirationHandler: method (of an UIApplication object), but it is intended to finish a finite-length task before going suspended. All background tasks may expire (and get terminated) before it finishes its job. You can check how much longer your application can run by checking backgroundTimeRemaining property of the application object.
As I explained here iOS receive Media Player Notifications when app is in background, there seems no way to get notifications from iPodMusicPlayer.
About Omar Raul Qazi answer:
a) i tried and I had no success. The music kept going down when pressing home button. I think this background flag only works for normal AudioSessions and not for MPMusicPlayer...
b) I am not sure this would work and I don't think Apple would like it when looking for approval
c) You can run in background only synchronous task. You cannot wait there for a notification. Am I wrong?
I'm making a Web app (at chirpid.com) for the iPhone that plays audio files for cricket chirp identification. The user can start and stop the audio by tapping screen buttons. But if the user taps the home button while a sound file is playing, it continues to play in the background (an iOS4 feature). I want to stop the audio in this case. Is there an event or property that I can use via Javascript in Safari to determine when I have been put into the background?
You could manually trigger the "stop" button like this when closing the application.
- (void)applicationDidEnterBackground:(UIApplication *)application {
if(audioIsPlaying) {
[btnStop sendActionsForControlEvents:UIControlEventTouchUpInside];
}
}
You get 5 seconds to do anything when using the applicationDidEnterBackground: function.
That should be enough to stop the audio ;-).
I would guess you need to create a listener that listens to system events...
Perhaps you should read this book: http://books.google.com/books?id=a09NMFdA6m0C&pg=PA94&lpg=PA94&dq=iOS+home+button+event+javascript&source=bl&ots=4Nop45gmwI&sig=8mTEGQI1ym65H7XjmBSzRQ2KyMc&hl=en&ei=_qDWTrmMDsPh4QSR9I2rAQ&sa=X&oi=book_result&ct=result&resnum=6&ved=0CEsQ6AEwBQ#v=onepage&q=iOS%20home%20button%20event%20javascript&f=false
iOS4 has some kind of lock function you can assign to buttons, perhaps you could tie in a script to turn down volume that way.
If you found the solution be nice and post it here for others to see.
Best of luck!
I'm using the SoundEngine provided with Apple's crash landing example.
After an interruption such as an incoming phone call or an alarm I call to applicationWillResignActive: inside my delegate, in order to pause the game and save the state of it. After the interruption ends I return to my game but the sound is gone. Even if i reinitialize it with SoundEngine_Initialize() the game still wont reproduce any of the sounds, unless i restart my app.
How can I restore my game sounds after the interruption ?
Have you looked at the sound manager class used by 71squared ? You can likely just use their sound manager as is... but if you wanted to role your own, you can look at thier code as I know they have solved this in their code.
http://www.71squared.com/2010/01/latest-sound-manager-class/