How do I put iOS application in background mode, with audio option set?
So my application for iphone4 reads data from the accelerometer and sends it to another application via tcp sockets. I need my app to work in backgro开发者_运维问答und mode, so what I did was:
- I put an mp3 file in the application's Documents folder
- I used AVAudioPlayer library to play the file in a loop. It works.
- I edited Info.plist and added option "required background modes" with "audio" on.
Still, the scheduler suspends the application whenever I press the iphone's home button. Is there anything I missed?
I read apple's documentation, but I didn't find a solution. A few thoughts on this:
- do I have to edit appDelegate.m?
- is it because I use AVAudioPlayer instead of the iPod?
- is it because I play an audio file from the application documents folder?
- I read about one person changing iOS Development Target from 4.0 to 3.2.1, but that didn't work for me.
And finally, say I get this to work, would the application still be getting data from the accelerometer?
On a side note, I don't want to submit the application to the App Store.
No, you will not receive accelerometer notifications in background mode. As far as I know, it is not possible. Check Executing Code in Background.
If you read the docs carefully, you will know that the whole background code model is based on responding to specific events (location
and voip
modes).
As for the audio
mode here is an extract from Apple:
Your application should limit itself to doing only the work necessary to provide data for playback while in the background. For example, a streaming audio application would download any new data from its server and push the current audio samples out for playback. You should not perform any extraneous tasks that are unrelated to playing the content.
Not sure whether you have solved your issue or not since this question was posted more than one year ago. Also, not sure whether playing audio is a must in your app or not. If both answers are no, my recent investigation may help a bit.
Here are how I get my app getting accelerometer data at the background 1. Follow this tutorial http://mobile.tutsplus.com/tutorials/iphone/ios-multitasking-background-location/ to get the background location working. 2. Follow this tutorial http://jonathanhui.com/ios-motion to get the accelerometer working.
Then you can get an app collecting accelerometer data at the background. Hope this helps.
精彩评论