Play Video in Background Using Avplayer

Is it possible to play video using Avplayer in Background?

This method supports all the possibilities:

  • Screen locked by the user;
  • List item
  • Home button pressed;

As long as you have an instance of AVPlayer running iOS prevents auto lock of the device.

First you need to configure the application to support audio background from the Info.plist file adding in the UIBackgroundModes array the audio element.

Then put in your AppDelegate.m into

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

these methods

[[AVAudioSession sharedInstance] setDelegate: self];    
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];

and #import < AVFoundation/AVFoundation.h >

Then in your view controller that controls AVPlayer

-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}

and

- (void)viewWillDisappear:(BOOL)animated
{
[mPlayer pause];
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
}

then respond to the

 - (void)remoteControlReceivedWithEvent:(UIEvent *)event {
switch (event.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
if([mPlayer rate] == 0){
[mPlayer play];
} else {
[mPlayer pause];
}
break;
case UIEventSubtypeRemoteControlPlay:
[mPlayer play];
break;
case UIEventSubtypeRemoteControlPause:
[mPlayer pause];
break;
default:
break;
}
}

Another trick is needed to resume the reproduction if the user presses the home button (in which case the reproduction is suspended with a fade out).

When you control the reproduction of the video (I have play methods) set

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];

and the corresponding method to be invoked that will launch a timer and resume the reproduction.

- (void)applicationDidEnterBackground:(NSNotification *)notification
{
[mPlayer performSelector:@selector(play) withObject:nil afterDelay:0.01];
}

Its works for me to play video in Backgorund.
Thanks to all.

AVPlayer play video in background

Just add [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; and some other tweaks. It's all here.

While using AVPlayerViewController, video plays in the background when user hits home button, but when returning to app the video closes by itself

In your video player class you need to add

delegate = self

and then

func playerViewControllerShouldAutomaticallyDismissAtPictureInPictureStart(_ playerViewController: AVPlayerViewController) -> Bool {
return false
}

iOS Enable AVPlayer to continue playing in background

Yes AVPlayerLayer might be creating issue. You need to remove it from AVPlayer object. Set to nil before application go to background.

Two ways to resolve according to Apple Document,

  1. Disable the video tracks in the player item (file-based content only).

  2. Remove the AVPlayerLayer from its associated AVPlayer (set the
    AVPlayerLayer player property to nil).

Refer this link for more info.

Playing a video in the background with AVPlayer giving problems with AspectFill and looping

You are using the wrong value for video gravity please change to:

playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;

Note that this value is for videos where as the value you used is used for rescaling images.

And for getting your video to loop you have just forgotten to rewind the video after it has finished playing. I don't use swift but in Objc C your loopVideo function would look like this:

-(void) loopVideo {
[self.moviePlayer seekToTime:kCMTimeZero];
[self.moviePlayer play];
}


Related Topics



Leave a reply



Submit