1

When playing an .mp4 locally from device the media loops the playback as expected. When the media is from a server no notification is fired when the video stops playing

AVPlayer *avPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:@"http://www.someurl.com/someMedia.mp4"]];
AVPlayerLayer *avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:avPlayer];
avPlayerLayer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.view.layer addSublayer:avPlayerLayer];

[avPlayer play];


// Loop video
avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(playerItemDidReachEnd:)
                                             name:AVPlayerItemDidPlayToEndTimeNotification
                                           object:[avPlayer currentItem]];




- (void)playerItemDidReachEnd:(NSNotification *)notification {

    NSLog(@"Video ended");

    AVPlayerItem *p = [notification object];
    [p seekToTime:kCMTimeZero];
}

Checked Looping a video with AVFoundation AVPlayer? and How do I loop a video with AVFoundation without unwanted pauses at the end?

UPDATE

Thanks to ChrisH for this, creating a delay to allow for the video to be loaded from the server before observing player lops the video correctly

double delayInSeconds = 7.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

        avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(playerItemDidReachEnd:)
                                                     name:AVPlayerItemDidPlayToEndTimeNotification
                                                   object:[avPlayer currentItem]];



    });
Community
  • 1
  • 1
JSA986
  • 5,870
  • 9
  • 45
  • 91
  • Is `currentItem` set on your `avPlayer` object when you add the observer? It's probably taking a little longer to load the item from a remote URL. – ChrisH Sep 22 '15 at 17:49
  • Not sure what you mean `currentItem` set on your `avPlayer` , everything in the pasted code is "as is". The looping file is four seconds loading time time is near instant, plus the `NSNotification is never called ` – JSA986 Sep 22 '15 at 17:55
  • 1
    You are instantiating `AVPlayer` with an `NSURL` and then immediately observing notifications from that instance's `currentItem` which may still be nil. The `currentItem` will not be set until `avPlayer` has loaded the content from the server. – ChrisH Sep 22 '15 at 18:17
  • 1
    Ah ok now it makes sense and is totally correct, can you make that an answer and I shall mark it as correct. Many thanks for that – JSA986 Sep 22 '15 at 18:29
  • No worries - answered! – ChrisH Sep 22 '15 at 18:51

1 Answers1

0

The reason that the notification is not firing for a remote mp4 is that you are observing notifications from the AVPlayer instance's currentItem immediately after the player is created, and the currentItem will be nil until the content has been loaded from the server.

The recommended way to handle this is to observe the status property of the player instance, and once that status equals AVPlayerStatusReadyToPlay you can start observing notifications from the currentItem. There's a good explanation along with sample code in this SO answer

Community
  • 1
  • 1
ChrisH
  • 4,468
  • 2
  • 33
  • 42