简体   繁体   English

当iPhone锁定/解锁且应用程序处于后台时如何恢复音频

[英]How to resume audio when iphone is locked/unlocked and the application was in background

This is the case: I made an app using the Audio Streamer library that reads an audio file from a remote server, but I find a problem only if I do this: 就是这种情况:我使用Audio Streamer库制作了一个应用程序,该从远程服务器读取音频文件,但是只有在执行以下操作时,我才发现问题:

  1. Launch the app 启动应用
  2. Start a Podcast (audio stream) 开始播客(音频流)
  3. Pause it 暂停一下
  4. Put the app on the background (home button) 将应用程序放在后台(主页按钮)
  5. Lock the phone 锁定手机
  6. Unlock it 解锁
  7. Reactivate the app 重新激活应用

And only then, my stream will be stopped. 只有这样,我的信息流才会停止。 I am attempting to reach the paused state, but could not get it done. 我试图达到暂停状态,但无法完成。

This happens only if I put the app in background, if not and I lock/unlock the iPhone, everything is alright again. 仅当我将应用程序置于后台时,才会发生这种情况;否则,如果我锁定/解锁了iPhone,一切都会恢复正常。 If I pause the stream then put the app into background (Home button) and then the app is resigned from background-state, everything is OK. 如果我暂停流,然后将应用程序置于后台(“主页”按钮),然后该应用程序从后台状态退出,一切正常。

So, this problem only occurs if these two things happen: App sent to background + Lock/unlock the iphone 因此,仅在发生以下两种情况时才会出现此问题:将应用发送到后台+锁定/解锁iPhone

does it use AVAudioPlayer under the hood? 它在后台使用AVAudioPlayer吗? If so you need to handle the AVAudioPlayerDelegate protocols: 如果是这样,则需要处理AVAudioPlayerDelegate协议:

- (void) audioPlayerBeginInterruption: (AVAudioPlayer *) player {

and

- (void) audioPlayerEndInterruption: (AVAudioPlayer *) player {

.. methods. .. 方法。 Essentially use the first to store the fact that the AVAudioPlayer was stopped due to an interruption, and the second to start it off again. 本质上,第一个用于存储AVAudioPlayer由于中断而停止的事实,第二个用于再次启动它。 Fiddly, but unfortunately neccessary. 很奇怪,但是很不幸地是必要的。

Here's a link https://developer.apple.com/documentation/avfoundation/avaudioplayerdelegate 这是一个链接https://developer.apple.com/documentation/avfoundation/avaudioplayerdelegate

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

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