简体   繁体   English

iPhone锁定/解锁时如何暂停/恢复音频

[英]How to pause/resume audio when iphone is locked/unlocked

Here's what currently happens in my app: 这是我的应用程序当前发生的情况:

Locking: iPhone Lock Button Pressed -> Audio Paused 锁定:按下iPhone锁定按钮->音频已暂停
Unlocking: iPhoneUnlocked Button Pressed -> Audio Resumed -> Slide to Unlock -> App Appears 解锁:按下iPhone的解锁按钮->恢复音频->滑动以解锁->出现应用

I want the Unlocking sequence to be: 我希望解锁顺序为:

Unlocking: iPhoneUnlock Button Pressed -> Slide to Unlock -> Audio Resumed -> App Appears 解锁:按下iPhone解锁按钮->滑动以解锁->恢复音频->出现应用

I am using OS 2.2.1 and AVFoundation Framework, and here's what I have already tried: 我正在使用OS 2.2.1和AVFoundation Framework,这是我已经尝试过的方法:

  • I tried to use applicationWillResignActive and applicationDidBecomeActive callbacks of the AppDelegate, it doesn't work. 我试图使用AppDelegate的applicationWillResignActive和applicationDidBecomeActive回调,它不起作用。 Although applicationDidBecomeActive is called at the correct time (ie when user has slided to unlock the device), the audio gets resumed as soon as unlock button is pressed (I guess it is because of the audio sessions), so I tried: 尽管applicationDidBecomeActive在正确的时间(即,用户滑动以解锁设备时)被调用,但是只要按下解锁按钮,音频就会恢复(我猜这是由于音频会话),所以我尝试了:

  • I implemented audioPlayerBeginInterruption and audioPlayerEndInterruption selectors of the AVAudioPlayerDelegate, it looks like these methods never get called (I have placed log calls). 我实现了AVAudioPlayerDelegate的audioPlayerBeginInterruption和audioPlayerEndInterruption选择器,看起来这些方法从未调用过(我已经放置了日志调用)。 I'm sure I am setting the delegate properly since audioPlayerDidFinishPlaying is correctly called when an audio file has finished playing. 我确定我正确设置了委托,因为音频文件播放完毕后会正确调用audioPlayerDidFinishPlaying。

PS: I am testing on device if that wasn't obvious PS:如果不是很明显,我正在设备上进行测试

They apparently only get called when an Audio session was interrupted, that's to say if you were playing audio and it is being interrupted by other audio which requests an active session. 显然,它们仅在音频会话被中断时才被调用,也就是说,如果您正在播放音频,并且正在被请求活动会话的其他音频所中断。 In short: they dont get called when the phone becomes inactive, but they get called when you receive a call for example. 简而言之:例如,当电话变为非活动状态时,它们不会被呼叫,但是当您接听电话时,它们会被呼叫。 But even then they only get called when the AVAudioplayer was active at the time. 但是即使那样,它们也只会在当时AVAudioplayer处于活动状态时被调用。 I'm still wrestling with that crappy system... 我仍然在为那个糟糕的系统而努力...

Slight abuse of the system, but you can observe the file protection notifications to find out when the device was unlocked. 轻微滥用系统,但是您可以观察文件保护通知,以了解何时解锁设备。 You could use that to decide when to play the audio again. 您可以使用它来决定何时再次播放音频。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM