如何阻止 MPMusicPlayerController 启用屏幕锁定
我有一个应用程序需要 iPhone 屏幕保持活动状态(或不保持活动状态,具体取决于用户选择)。我通过禁用应用程序空闲计时器来完成此操作,在我开始通过 MPMusicPlayerController 播放媒体之前,该计时器工作得很好。由于 SDK 中的错误,这会重新启用空闲计时器,但没有明显的方法可以再次禁用它。
我的应用程序流程是:
- 应用程序启动
- 屏幕保持打开
- 状态 <...时间流逝...>
- 播放音频文件
- 空闲计时器启动
- 屏幕关闭
我在后台播放一个空的音频文件,以阻止手机进入深度睡眠状态,但我真的很想让屏幕也保持解锁状态。
有没有人设法找到解决方法?
I have an application that requires the iPhone screen to remain active (or not, depending on user choice). I've done this by disabling the application idle timer, which works fine and dandy until I start playing media via the MPMusicPlayerController. Due to a bug in the SDK, this then reenables the idle timer with no apparent way to disable it again.
My app flow is:
- App starts
- Screen stays on
- <...time passes...>
- Play audio file
- Idle timer kicks in
- Screen turns off
I have an empty audio file playing in the background to stop the phone going into deep sleep, but I'd really like to keep the screen unlocked too.
Has anyone managed to figure out a workaround for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我遇到了类似的问题,并找到了解决方法。该修复可能也适合您:
我定期(每 10 秒)调用一个方法,该方法首先将idleTimerDisabled 设置为“NO”,然后设置为“YES”。
仅单独设置为 YES 并不能解决问题。看来必须首先更改属性才能被 UIApplication 识别。
我的问题是,当我通过耳机遥控器切换 iPod 播放器上的音乐曲目时,屏幕一直变暗。我的猜测是,这与您遇到的问题相同。
I had a similiar problem, and found a fix for it. The fix might work for you too:
I call a method periodically (every 10 seconds), which sets idleTimerDisabled first to NO, then to YES.
Only setting to YES alone does not fix the problem. It seems the property has to change first to be recognized by UIApplication.
My problem was, that the screen kept turning dark as soon as I switched music tracks on the iPod player via the headphone remote. My guess is, that this is the same issue as you are experiencing.
您只需关闭空闲计时器即可。我通常在需要保持“唤醒”的视图控制器中执行的操作是:
这将确保屏幕不会由于用户不活动而被锁定。
You should simply turn off the idle timer. What I usually do in a viewcontroller that needs to stay 'awake' is this:
This will make sure the screen will not get locked due to user inactivity.
我找到了解决这个问题的办法。开始播放音乐后约 5 秒调用禁用idleTimer 的方法。这有点像黑客,但它是一种解决方法。
I found a solution to this problem. Invoke a method that disables the idleTimer in about 5 seconds after you start playing the music. It's a bit of a hack, but it is a workaround.