音频不会继续在后台播放

时间:2014-04-22 16:17:11

标签: ios avplayer appdelegate

在我的应用中,我有一名玩家:

@property (nonatomic, strong) AVPlayer *player;

我希望在我的应用处于后台时播放音频。要做到这一点,我有:

在MYAPP-Info.plist

在我的AppDelegate .h:

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

在我的AppDelegate .m:

    // Set AudioSession
    NSError *sessionError = nil;
    [[AVAudioSession sharedInstance] setDelegate:self];
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];

    /* Pick any one of them */
    // 1. Overriding the output audio route
    // UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
    // AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, sizeof(audioRouteOverride), &audioRouteOverride);

    // 2. Changing the default output audio route
   UInt32 doChangeDefaultRoute = 1;
   AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryDefaultToSpeaker, sizeof(doChangeDefaultRoute), &doChangeDefaultRoute);

不幸的是,音频不会继续在后台播放。有任何想法吗 ?

1 个答案:

答案 0 :(得分:0)

尝试添加..

 [[AVAudioSession sharedInstance] setActive: YES error: &activationError];

在您的代码之后..

NSError *sessionError = nil;
NSError *activationError = nil;
[[AVAudioSession sharedInstance] setDelegate:self];
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&sessionError];

此外,我发现您已经进行了更改以在后台模式中启用音频和播放,但是您是否已将应用程序设置为不在后台运行?

enter image description here