我正在尝试让我的AVPlayer在后台播放音频,即使屏幕被锁定。现在,它确实在屏幕锁定时播放,但一旦解锁,它就会停止。
在申请代理中,我有
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
我通过播放声音
AVPlayer *player = [[AVPlayer alloc]initWithURL:self.mp3URL];
_audioPlayer = player;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[_audioPlayer currentItem]];
[_audioPlayer play];
我在info.plist中还将所需的背景模式设置为"应用程序播放音频"。有什么想法吗?
在您发布的代码中没有任何原因。也许您正在重新激活音频会话,导致它在视图显示时停止?错误不在您发布的代码中。