How do I enable background audio for an iPhone app - iphone

Currently I am designing an iOS application that will connect to a music stream through a network and play the audio to the user.
I have a simple setup with a button the enables the stream to start, and a UIwebview that connects to the stream. When I run the app (on an iPhone, NOT a simulator), the button works fine and launches the Quicktime player to begin playback for the audio. Pausing and playing from this screen works like a charm as well.
However I want my user to be able to start up the stream, turn the phone off (sleep the display) and continue to listen to the stream. However sleeping the display will fade out the audio until it stops playback.
I have tried to go into the app's PList file like a few others have told me to do online and added the field "Required background modes" and added App plays audio or streams audio/video using Airplay to the 0 array field and App downloads content from the network to the 1 array field.
("App plays audio" was not offered through auto-complete even though that was the phrase told to make the stream work. Instead I left it as "App plays audio or streams audio/video using Airplay" before trying it the other way to little more luck)
However neither of these are allowing the audio to continue to play when the display has been put to sleep. Can anyone offer up a suggestion as to how to make it work?

In Xcode 5.1, there's another place aside from the Plist that this needs to be set, Target -> Capabilities -> Background Modes... this seems to do more than just affect the plist, though I'm not absolutely sure of this.

Look here an sample code https://github.com/jsagorin/iOSBackgroundAudio
and here .. some explanations (how to Set UIBackgroundModes key in app-info.plist file, Set Audio Session Category , etc) http://www.sagorin.org/ios-playing-audio-in-background-audio/

Just make an entry in plist
Application does not require background mode and set its value to 'NO'
And add background Mode to VOIP

Related

Using a webview to play an mp3- is it possible to keep it playing even when the screen is locked?

I have a portion of my app that simply launches a webview linked to an mp3 file- it opens the file and plays the audio. What I'd like to do is allow that audio to continue playing even if the user locks their iPhone. (As a side note, is there any way to replace the default Quicktime logo that appears in the player? Not that big a deal, just thought I'd ask =)
A third question- I also do the same thing (webview) with an .m3u linked file to stream live audio throughout the week- is it also possible to keep it playing when the iPhone is locked?
Thanks!!
Ben
Quick Edit: The same URLs work great in IOS Safari (locking and unlocking doesn't affect the audio. Just not in my app's webviews.
I did find my answer though, in case anyone comes here looking. Easiest thing in the world too- just add a row to the plist file labeled "Required Background Modes" and under that set item 0's value to "App plays audio". Voila!
Since the audio is in a UIWebView, I don't believe that you'd be able to play it in the background.
EDIT: I was wrong, adding "App Plays Audio" to the UIBackgroundModes will make it work even when audio is in a UIWebView.
Have you thought about just streaming the .mp3 file yourself? Should be easy enough and you'd be able to setup background streaming. To get you started, I'd check out Matt Gallagher's AudioStreamer class which easily allows you to stream .mp3 files and other audio files from the net: https://github.com/mattgallagher/AudioStreamer

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

Responding to events when device has been locked

I'm working on a radio alarm clock, and i have some issues.
I am using local notifications for the alarms, so it has a gentle fallback if the app is not running.
I am well aware of the limitations of the device, and i know what i can and cannot do when the device has gone into background.
But my question is this:
I have seen other apps starting an audio streamer when i've locked the device. How is this possible? May this be inside an execution-timeframe?
How is the best way to implement this? Is it any way i can activate a streaming session when the device is locked?
Edit
To clarify: I know how i make audio play in the background. But the issue is triggering the audio-playback when an local notification or some other event fires.
One app that seems to do this, is Radio Alarm Clock. I haven't tried it for long period of times yet. But it seems to do this. A video demo of the app: http://www.youtube.com/watch?v=KJQiFOcdBWk
Have you already declared your background task?
Support for some types of background execution must be declared in advance by the app that uses them. An app declares support for a service using its Info.plist file. Add the UIBackgroundModes key to your Info.plist file and set its value to an array containing one or more of the following strings:
audio — The app plays audible content to the user while in the background. (This content includes streaming audio or video content using AirPlay.)
iOS App Programming Guide - Implementing Long Running Background Tasks
You can add this by clicking on your main project icon, then under the Info tab you can add "Required Background Modes" to the "Custom iOS Target Properties" section. "App Plays Audio" will be one of the three default values.
Big Edit With New Answer:
If everything else is already in order, you can keep your app running in the background using the UIApplication method
- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:(void (^)(void))handler
detailed here: UIApplication Class Reference
with an example here: Hour 21: Building Background-Aware Applications
This allows you to run an instance of NSTimer which triggers your music player. The difference between this approach and UILocalNotifications is that this method never lets the app fully enter the background mode, the music player exists the entire time which subverts the need to create it from the background, which looks to be impossible.
There may be limitations to how long of a timer you can set, I haven't tested this past 14 minutes out.

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! :)

How to record a video automatically in Iphone app without user interaction

I am working an iphone app that needs to record a vedio automatically.
I used mobile coreservices framework and using that. I made it to came into video mode and clicking on record option its start capturing a vedio. But I want it automatically that is.. I should able to record a video without clicking on record option. That is when video mode comes up its automatically start record video.
Could any one help?
You can look at UIImagePickerControllers startVideoCapture method which is used to start taking video from the camera, this is to be used when you arent using the camera standard controlors and you provide and overlay view. Here is a reference UIImagePickerCOntroller ref. If this is not enough for you, you might want to look into AVFoundation framework which gives you a lot more control over video capturing process...hope that helps