iphone - 即使类别是 AVAudioSessionCategoryPlayback,AVAudioPlayer 也会在屏幕锁定时停止播放

标签 iphone screen avaudioplayer

我的应用程序使用 AVAudioPlayer 来播放音频播放列表。我在iOS4.3和iOS5之间没有更改过代码。但是,即使我将类别设置为 AVAudioSessionCategoryPlayback,iOS5 上的音频也会在屏幕锁定时暂停。

我在iOS4.3和iOS5设备上测试了代码,在iOS5设备上出现了这个问题。

还有其他人遇到过这个问题吗?我很茫然,因为我什至不知道从哪里开始调试这个问题。我的调试从检查 AVAudioSessionCategory 设置开始和结束。

我正在执行的代码是:

[[AVAudioSession sharedInstance] setDelegate: self];
NSError *setCategoryError = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &setCategoryError];

编辑:

我已经实现并处理了audioPlayerBeginInterruption

编辑#2:已解决!感谢Rhythmic Fistman's Answer

这就是发生的事情以及我所做的事情。 iOS5 的更改现在会将您的应用程序推到后台,即使屏幕被锁定也是如此。这意味着您需要在应用程序中启用背景音频,并使处理该音频的任何 View / View Controller 成为第一响应者。

第 1 步: Enable background audio in your app

第 2 步: Enable remote events and become the first responder on the viewcontroller / view responsible for handling your audio playlist

注意: 请务必在设备上进行测试。模拟器会让代码看起来好像不起作用。

最佳答案

iOS5 中的屏幕锁定已更改,它现在会将您的应用程序发送到后台,因此如果您希望音频继续播放,则必须将“音频”作为 UIBackGroundModes 添加到您的 Info.plist 文件中。

我能找到的最接近于此的文档是一些 苹果devforum posts (需要登录)。

关于iphone - 即使类别是 AVAudioSessionCategoryPlayback,AVAudioPlayer 也会在屏幕锁定时停止播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8085049/

相关文章:

ios - UILabel字体需要花费时间才能更新

iphone - 哪些脚本语言解释器可以在 iOS 上运行?

ios - iOS AVAudioPlayer无法播放

iphone - 更有效的代码? (AVAudioPlayer)

ios - 以下 : UIViewController, UINavigationController、UITabController 的通用返回方式

iphone - 在 iPhone 或 iPad 应用程序中调用 main 时会发生什么?

安卓开发 : Scale to fit different devices

c++ - 如何在 ROOT (CERN) 中禁用屏幕输出

javascript - 根据屏幕分辨率运行 jquery 代码的正确方法

ios - AVAudioPlayer 的属性给出 "Cannot assign result of this property"