当iPhone进入睡眠状态时,AVAudioPlayer停止播放音频

时间:2009-05-25 23:23:42

标签: iphone

只要我的应用程序正在运行,我就需要一直播放背景声音。我试图将AVAudioPlayer用于此目的。但是,一旦iPhone进入睡眠模式,它就会停止播放声音。

你能让我知道如何解决这个问题吗?

由于

3 个答案:

答案 0 :(得分:5)

您可能需要设置音频会话类别。来自“iPhone应用程序编程指南”

  

类别是标识a的键   你的音频行为集   应用。通过设置类别,   你表明你的音频意图   iPhone OS,如你的音频   屏幕锁定时应该继续。

有关每个类别的详细信息,请参阅“Audio Session Categories

答案 1 :(得分:4)

在您的特定示例中,您可以尝试以下操作。它应该开箱即用。

// Registers this class as the delegate of the audio session.
[[AVAudioSession sharedInstance] setDelegate: self];    
// Allow the app sound to continue to play when the screen is locked.
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

答案 2 :(得分:1)

这个解决方案也可能有帮助

    [[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/

相关问题