我有一个播放Spotify曲目的应用程序。当应用程序移动到后台时,它可以完美运行,但是当它被"呼叫"或"iOS弹出窗口"打断时,它会停止播放。我已经在 .plist 中添加了Required background modes
App plays audio
。如何在应用程序处于后台时中断结束时恢复播放。
我使用了音频中断回调。
AudioSessionInitialize (NULL, NULL, interruptionListenerCallback,self);
AudioSessionSetActive(YES);
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
并在收到播放管理器的中断呼叫后播放/暂停方法。
void interruptionListenerCallback (void *inUserData, UInt32 interruptionState)
{
// This callback, being outside the implementation block, needs a reference
//to the AudioPlayer object
CustomPlayerView *controller = (__bridge CustomPlayerView *) inUserData;
if (interruptionState == kAudioSessionBeginInterruption)
{
[controller.playbackManager setIsPlaying:NO];
}
else if (interruptionState == kAudioSessionEndInterruption)
{
[controller.playbackManager setIsPlaying:YES];
}
}