Skip to content
This repository has been archived by the owner on Dec 30, 2023. It is now read-only.

How to get AVPlayer, to play while app is in background

Notifications You must be signed in to change notification settings

PatrickRorth/iOS-Background-AVPlayer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 

Repository files navigation

iOS - Background player for AVPlayer

Replace APPNAME with your own app name!

Add UIBackgroundModes in the APPNAME-Info.plist, with the selection App plays audio

Then add the AudioToolBox framework to the folder frameworks.

In the APPNAMEAppDelegate.h add:

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

so it look like this:

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

In the APPNAMEAppDelegate.m add the following:

// 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);

into

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

but before the two lines:

[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];

Build your project and see if theres any error, If not, try debug on Device insted of the Simulator, the simulator can bug.

Hope this helps.

Find other solutions here

About

How to get AVPlayer, to play while app is in background

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published