Play audio from app, even when it is in the backgr

2019-01-20 15:12发布

I am having difficulties determining how I would play audio in the background for my iOS app. I have a NSTimer and wish to have a sound be played when it reaches 5 minutes, even when in the background. I already have the audio background mode enabled, but unsure how to accomplish my idea.

1条回答
太酷不给撩
2楼-- · 2019-01-20 16:04

Under capabilities select the following:

enter image description here

In your plist make sure where it says "Application does not run in background" is NO.

enter image description here

Then add the following to your your appDelegate.m file.

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

In the following method:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    return YES;
}
查看更多
登录 后发表回答