iPhone正在睡觉时音频无法播放

时间:2009-08-11 11:46:50

标签: iphone

我想知道一件事。我希望你的人能指导我。

我正在创建一个需要一些音频警报的应用程序。我为此目的使用AVAudioPlayer

我还设置了这样的音频会话(在我的主委托类中):

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

如果我的屏幕被强行锁定,我可以播放音乐,但如果iPhone自动进入睡眠模式,我将无法播放mp3文件。

一般情况下,10分钟后,我的应用程序中没有任何音乐。谁能告诉我我错过了什么?如果你知道我的问题,请指导我。


编辑1

我目前正在使用kAudioSessionCategory_MediaPlayback。 我在我的代码中写了它,我把它贴在这里。


编辑2

我想从睡觉的iphone开始播放音频。

如果你知道,请帮助我。


编辑3

idleTimerDisabled,消耗我的电池?

我正在创建一个警报,因此该应用程序可以运行长达10个小时。 它会禁用屏幕上的灯光吗? 然后我可以使用它。 我已经看到一些在iPhone上运行的应用程序,它从睡眠模式播放音乐。

让我试试这个,谢谢你的回复。

4 个答案:

答案 0 :(得分:4)

我在this thread here以及this thread on SO中找到了答案。

正在发生的事情是iPhone在10分钟后(屏幕锁定后)进入深度睡眠模式,停止NSTimer并进入低功耗模式。即使您将类别设置为MediaPlayback,它也会执行此操作,除非您实际播放声音。给出的解决方案是定期播放“静音”声音文件,以防止它进入深度睡眠模式。

这是我在深度睡眠发生时在控制台日志中看到的内容(警告来自我的时钟应用程序):

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

答案 1 :(得分:0)

您需要使用其他音频会话类别。您可以查看文档here

我认为您应该使用的是kAudioSessionCategory_MediaPlayback

答案 2 :(得分:0)

为防止您的应用在 n 分钟后沉默,您可能还需要将UIApplication的{​​{1}}属性设置为idleTimerDisabled

答案 3 :(得分:0)

你应该试试这个

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

http://www.mindyourcode.com/ios/iphone/how-to-play-audio-in-iphone-sleep-mode/

的教程中引用
相关问题