sound and Nstimer stopped when iphone is in deepsleepmode?

安稳与你 提交于 2019-11-28 10:37:21

In order to prevent an app from going to sleep when the screen is locked, you must set your audio session to be of type kAudioSessionCategory_MediaPlayback.

Here's an example:

UInt32 category = kAudioSessionCategory_MediaPlayback;
OSStatus result = AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,
                                        sizeof(category), &category);

if (result){
    DebugLog(@"ERROR SETTING AUDIO CATEGORY!\n");
}

result = AudioSessionSetActive(true);
if (result) {
    DebugLog(@"ERROR SETTING AUDIO SESSION ACTIVE!\n");
}

If you don't set the audio session category, then your app will sleep.

This will only continue to prevent the app from being put to sleep as long as you continue to play audio. If you stop playing audio and the screen is still locked, the app will go to sleep and your timers will be paused.

If you want the app to remain awake indefinitely, you'll need to play a "silent" audio file to keep it awake.

I have a code example of this here: Preventing iPhone Sleep

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!