Otto Boy Otto Boy -3 years ago 251
iOS Question

Not able to play youtube videos in background on iOS, using YTPlayerView

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];
if (setCategoryErr)
NSLog(@"Setting Audio Session Category Error: %@",[setCategoryErr description]);
[audioSession setActive:YES error:&activationErr];
if (activationErr)
NSLog(@"Activating Audio Session Error: %@",[activationErr description]);

but however video pauses when app enters background. What else should I do to make it work?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download