2

I would like to know one thing. I hope you people can guide me well.

I am creating an application which needs some audio alerts. I am using AVAudioPlayer for this purpose.

I also set the Audio session like this (in my main delegate class):

//to play from sleep
OSStatus result = AudioSessionInitialize(NULL, NULL, interruptionListenerCallback, self);
UInt32 category = kAudioSessionCategory_MediaPlayback;
result = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(category), category);
AudioSessionSetActive(YES); 

I am able to play music if my screen is locked forcefully, but I am not able to play mp3 files if the iPhone go into sleep mode automatically.

In general after 10 minutes I am not getting any music from my application. Could anyone tell me what I am missing? Please guide me if you know about my problem.


EDIT 1

I am currently using kAudioSessionCategory_MediaPlayback. I written it in my code, i pasted it here.


EDIT 2

I want to start a audio play back from a sleeping iphone.

Please help me if you know.


EDIT 3

idleTimerDisabled , consume my battery ?

I am creating a alarm so this application can run up to 10 hours. It will disable light on screen? then i can use it. I already seen some application which running on iPhone which play music from sleep mode.

Let me try this, thank you for the reply.

Welbog
  • 59,154
  • 9
  • 110
  • 123
  • When you need to add more details about the problem you can just edit your question by pressing the small `edit` link under the question's tags. You can also comment on any answer to your question to ask more details from their authors. – Welbog Aug 11 '09 at 12:39
  • I have this same problem. I have the category set to MediaPlayback, I don't want to disable the idleTimer because I want the screen to lock. The screen locks after 2 minutes automatically, but according to the console log the system goes to sleep after 10 minutes. I want to play a chime every 15 minutes and it doesn't work. – progrmr Dec 24 '09 at 17:39

4 Answers4

4

I found the answer already on SO in this thread here and also in this thread on SO.

What's happening is that the iPhone goes into deep sleep mode after 10 minutes (after the screen is locked) where it stops your NSTimer's and goes into lower power mode. Even when you have the category set to MediaPlayback it will do this unless you are actually playing a sound. The solution given is to play a "silent" sound file periodically to prevent it from going into deep sleep mode.

Here's what I see in the console log at the time deep sleep occurs (the Warnings are from my clock app):

Thu Dec 24 09:25:09 unknown Clock[16346] <Warning>: ClockDigital itemInterval=0 curInterval=0 needsUpdate=YES
Thu Dec 24 09:25:09 unknown Clock[16346] <Warning>: BatteryIcon itemInterval=6 curInterval=0 needsUpdate=NO
Thu Dec 24 09:25:09 unknown CommCenter[28] <Notice>: Telling CSI to go low power.
Thu Dec 24 09:25:09 unknown CommCenter[28] <Notice>: CSI can enter low power, so now telling to do so.
Thu Dec 24 09:25:09 unknown CommCenter[28] <Notice>: Will sleep.  Heard from CSI in 0.00501698 seconds
Thu Dec 24 09:25:10 unknown kernel[0] <Debug>: AppleSynopsysOTGCore::sleepWakeNotification: Sysmtem Going to sleep
Thu Dec 24 09:25:10 unknown kernel[0] <Debug>: AirPort: Disabled AppleBCMWLAN (link 2, sys 1, user 1)
Thu Dec 24 09:25:10 unknown kernel[0] <Debug>: AppleBCMWLAN::setPOWER() [kernel_task]: Setting power state to 0
Thu Dec 24 09:25:10 unknown kernel[0] <Debug>: AppleMultitouchN1SPI: disabled power
Thu Dec 24 09:25:10 unknown kernel[0] <Debug>: AppleBCMWLAN Left BSS:       @ 0xcf3cc800, BSSID = 00:0f:a3:1c:9f:0c, rssi = -53, rate = 54 (100%), channel =  7, encryption = 0x2, ap = 1, failures =   0, age = 26, ssid[ 8] = "mggm.ap1"
Thu Dec 24 09:25:10 unknown kernel[0] <Debug>: AirPort: Link Down on en0
Thu Dec 24 09:25:10 unknown kernel[0] <Debug>: AppleBCMWLAN::powerOff Ready to power off
Thu Dec 24 09:25:10 unknown kernel[0] <Debug>: AppleBCMWLAN::setPowerStateGated() : Powering Off and sleeping
Thu Dec 24 09:25:10 unknown kernel[0] <Debug>: AppleBCMWLAN::powerOff Ready to power off
Thu Dec 24 09:25:10 unknown configd[22] <Error>: WiFi:[283368310.000857]: Unable to dispatch message to client dataaccessd (0x10000004)
Thu Dec 24 09:25:10 unknown configd[22] <Error>: WiFi:[283368310.013365]: Unable to dispatch message to client apsd (0x10000004)
Thu Dec 24 09:25:10 unknown Clock[16346] <Warning>: ClockVC: adjusted interval: 0.993291
Thu Dec 24 09:25:10 unknown configd[22] <Error>: WiFi:[283368310.018146]: Error initiating scan request: 82
Thu Dec 24 09:25:10 unknown kernel[0] <Debug>: AppleBCMWLAN::setPOWER(): IOKit power off. Discarding request.
Thu Dec 24 09:25:10 unknown Clock[16346] <Warning>: CalendarDay itemInterval=5 curInterval=0 needsUpdate=NO
Thu Dec 24 09:25:10 unknown Clock[16346] <Warning>: BatteryState itemInterval=6 curInterval=0 needsUpdate=NO
Thu Dec 24 09:25:10 unknown Clock[16346] <Warning>: MindfulnessBell itemInterval=2 curInterval=0 needsUpdate=NO
Thu Dec 24 09:25:10 unknown Clock[16346] <Warning>: ClockDigital itemInterval=0 curInterval=0 needsUpdate=YES
Thu Dec 24 09:25:10 unknown Clock[16346] <Warning>: BatteryIcon itemInterval=6 curInterval=0 needsUpdate=NO
Thu Dec 24 09:25:10 unknown kernel[0] <Debug>: System Sleep
Community
  • 1
  • 1
progrmr
  • 75,956
  • 16
  • 112
  • 147
0

You need to use a different Audio Session Category. You can check the documentation here.

I think the one you should use is kAudioSessionCategory_MediaPlayback.

pgb
  • 24,813
  • 12
  • 83
  • 113
  • I'm using MediaPlayback category and still have the same problem. Sleeps after 10 minutes. I'm only playing a sound every 15 minutes which I think is part of the problem in my case anyway. – progrmr Dec 24 '09 at 17:41
0

To prevent your app from being silenced after n minutes, you might also need to set the UIApplication's idleTimerDisabled property to YES.

invalidname
  • 3,175
  • 21
  • 18
  • That keeps the screen from locking but also uses a lot of battery since the backlight is on. I think I read somewhere that playing audio will keep the system from sleeping after 10 minutes but can't find it right now. – progrmr Dec 24 '09 at 17:40
0

You should try this

    [[AVAudioSession sharedInstance] setDelegate: self];
 [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; 
[[AVAudioSession sharedInstance] setActive: YES error: nil];

referenced from a tutorial at http://www.mindyourcode.com/ios/iphone/how-to-play-audio-in-iphone-sleep-mode/

Mashhadi
  • 3,004
  • 3
  • 46
  • 80