AVPlayer在关闭屏幕时停止播放音乐

时间:2016-10-17 09:49:20

标签: ios avplayer

我的项目相关音乐,我在关闭屏幕时停止播放音乐时出现问题。虽然我注册了背景模式(音频)但有时当我关闭屏幕AVPlayer停止播放音乐并在打开屏幕时再次播放。

我花了很多时间,但我知道为什么会这样。

1 个答案:

答案 0 :(得分:0)

仅仅添加audio后台模式还不够。您还必须将播放音频会话类别设置为described in Apple QA1668

  

答:您必须声明您的应用在后台播放可听内容,并为您的音频会话分配适当的类别。

#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>

AVAudioSession *audioSession = [AVAudioSession sharedInstance];

NSError *setCategoryError = nil;
BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
if (!success) { /* handle the error condition */ }

NSError *activationError = nil;
success = [audioSession setActive:YES error:&activationError];
if (!success) { /* handle the error condition */ }