In my iOS app, I'm playing youtube videos using YTPlayerView (see this link for information), but video playback pauses if app has entered background. I would like to continue playing videos in background, I added "Audio and AirPlay" to "Required background modes" and set audio session in application delegate this way:
- (void) setAudioSession
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryErr];
NSLog(@"Setting Audio Session Category Error: %@",[setCategoryErr description]);
[audioSession setActive:YES error:&activationErr];
NSLog(@"Activating Audio Session Error: %@",[activationErr description]);
After many investigations, I've found that it's not possible. The following comment on this issue explains why:
Technically speaking YouTube is a VIDEO streaming service so naturally the video must be visible in order to also hear the audio. Therefore, entering background mode should also stop the video playback.
In order to comply with YouTube TOS and Apple guidelines you need to display the video - and stop it from playing when locking the screen. Sadly, no background playback of YouTube videos.