【问题标题】:AVPlayer stops playing when screen is unlocked (but works when locked)AVPlayer 在屏幕解锁时停止播放(但在锁定时工作)
【发布时间】:2013-08-07 00:57:17
【问题描述】:

我试图让我的 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 中将所需的背景模式设置为“应用程序播放音频”。有什么想法吗?

【问题讨论】:

    标签: ios avfoundation avplayer


    【解决方案1】:

    我认为您发布的代码中没有任何原因。也许您正在重新激活音频会话,导致它在视图显示时停止?错误不在您发布的代码中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-29
      • 1970-01-01
      • 1970-01-01
      • 2012-12-03
      • 2017-02-26
      • 1970-01-01
      相关资源
      最近更新 更多