iPhone playing sounds while in the background - iphone

I have a small application which is much like a clock. It has been working great on IOS3, but now I am updating it to iOS4.
What I want to do in iOS4 is to let it play a sound any giving time. For instance, if I set it to play at 4.00 PM I want it to play a sound, a sound from the application not from the OS. This works if the app is launced, but if the user went back to the home screen I want the same to happen.
It is OK if a UIAlertView pops up istead of the application.
How can I do this?
Best regards,
Paul Peelen

Sounds like you'll need to use a local notification:
http://developer.apple.com/iphone/library/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction/Introduction.html
and
http://developer.apple.com/iphone/library/documentation/iPhone/Reference/UILocalNotification_Class/Reference/Reference.html#//apple_ref/occ/cl/UILocalNotification

Read http://developer.apple.com/iphone/library/documentation/iphone/conceptual/iphoneosprogrammingguide/BackgroundExecution/BackgroundExecution.html.
What you want is UIApplication setKeepAliveTimeout:handler: which let you register a handler that will be called. You can use that to start playing audio. (Of course, compute the timeout when your app goes to background)
EDIT: Use UILocalNotification as andy said.

Two things you could possibly try under iOS 4.
Local Notifications can pop up an alert and play a short sound.
Another power hungry option is to register as a background music player, and play silence until time for the alarm to go off, then add your chosen sound to the "music" output. This will fail if the user switches to another media player app.

Related

iPhone: MPMusicPlayerController stops AVAudioPlayer

I'm playing a silent music with AVAudioPlayer when user locks the screen, so that my timers won't stop.
However, when I play an iPod music with [MPMusicPlayerController applicationMusicPlayer], AVAudioPlayer stops,without receiving any call back.
Is there any way so that I can start [MPMusicPlayerController applicationMusicPlayer] playing without stoping AVAudioPlayer playing?
EDIT:
Thanks guys, this is the app I'm working on:
It is an Alarm app, this app allows user to lock screen while app is running,and when it is the time of the alarm, app can play iPod music to wake the user.Local notification can not use iPod music as alert sound, so I have to keep the app running while screen is locked.
If user quit the app, it will use local notification as alarm, whose sound is limited to files in bundle.
I can't use UILocalNotification as timer since when in screen locked status(in UIApplicationStatusInactive), app can't receive local notification generated by the system.
Apple has architected their backgrounding system to really limit things like this from happening. Essentially, there is no way for the you to keep the application running in the background unless it needs to be there. If you explain what you are trying to accomplish, maybe a better solution can be found but as good practice, never use random backgrounding methods to do other things. I am assuming that you might be using the faint music as a way to show something custom on the main screen, this is not a good idea.
Your app will get rejected if you play a silent audio.
Also as per apple's documentation https://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103, notifications cannot have sounds (soundName) which play more than 30 seconds.
So you wont be able to release your app in the store.
I figured it out myself.
It is not calling [MPMusicPlayerController +applicationMusicPlayer] that stops AVAudioPlayer, but calling [MPMusicPlayerController -setShuffleMode:], I don't know why calling this would stop AVAudioPlayer, but it is where the problem lies in.
Thanks everyone, I think I should paste my complete code next time.

Playing loop sound at certain time when iphone app is in background

I have a Clock Alarm app that plays sound at certain time (Sending local notifications). Is it possible to play loop sound at certain time (when local notification comes) when app is placed in background ?
Just want to specify: I need to play loop sound, not only once
No. The reason why is because you won't be able to execute any code because of a local notification. One thing you can do, and it's totally up to you if this is something your app really needs to di, it to list this application as an audio application on the UIBackgroundModes key of your .plist file, and just change song that is playing at the time it has to. You can use a dispatch_after block to change the song at the time you want it to.
Make your app a long running background app as described in this link here

Assign alert tone for UILocalNotification from available sounds in iphone

I am working on an app that used UILocalNotification. I wanted to select sound from default sounds that came with iPhone. How can i list down sounds for assign as the alert tone.
There is one standard sound for notification from system (UILocalNotificationDefaultSoundName). You can play any custom sound shorter than 30 seconds. So, if you can download any native sound from ios from anywhere, you will be able to use it as custom sound. Documentation

Iphone sdk - How to play a sound during a phone call after some elapsed time?

Well I am having two issues that i can't get to work, related to audio and calls.
The first one is to play a sound during a phone call. I don't want to play continuous music or stream anything, it is just a simple and short sound that the user will hear at one time during his call.
I have read some posts claiming that this is possible, and I even have an application that does so, but I can't get it to work. My app identifies the call using CTCallCenter and print the logs but never plays the sound or plays it after the app comes to foreground again. I have the .plist property of required background mode App plays audio.
The second issue, is to play the sound after some elapsed time. NSTimers doesn't work when on background mode, nor NSThread sleep on my background process or NSOperation. So how could I play this sound after say 10 seconds of the call?
Also, this behavior has to work also when the application is already on background mode. With CTCallCenter I am only getting the event when the application is interrupted from use, but I don't see any logs when i send the app to background and then begin/receive a call
.
If anyone could point me to the right direction I'll be really grateful.
I havent done this, but NSLocalNotifcation, schedule a notification to play when you app get the call to move into the background. I would expect this to work. Interested to find out if it does.

view capturing in background

we can easily capture a view(screenshot) programmatically in iPhone.
But i am trying to capture screen automatically after every 5 seconds.that also, can be done.
But my main objective is , to capture screen, even if the app is closed, i.e in background.
Apple doesn't allow to background process to run for a long time, but can we do it for 5-10 min in background.
Capturing screen,even if the app is closed.Ofcourse user can close this function, within the app.
Apple might reject this, but is this possible ?
Regards
This is possible but it's not easy. You can register you app (via a .plist setting) as being a media app, this will allow it to run in the background. This is normally to allow media apps to continue playing music when the app is sent to the background. However if you are careful you can get it to do other thing in there too.
In your plist under "Required Background Modes" add a key "Item 0" and set it to "App plays Audio". This will mean you can do it.
You will then have to using AVAudioPlayer, play a silent sound that is say 5mins long, and then register one of your controllers for the AVPlayerItemDidPlayToEndTimeNotification notification. Then when this calls back, you can have your bit of code that, first takes a screenshot, and then starts the sound again.
This is the general concept, look somewhere like :
http://mobile.tutsplus.com/tutorials/iphone/ios-sdk_background-audio/
for how to set up the audio playing.
Just a note: APPLE WILL REJECT THIS!
If you are making an app for personal use only, this is fine, but will never make it to the app store.
Hoep this sheds some light on your situation! :)