Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8466739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:25:52+00:00 2026-06-10T15:25:52+00:00

I am looping a video within my avfoundation video player: NSString *loopPath = @SubView/introCycle;

  • 0

I am looping a video within my avfoundation video player:

NSString *loopPath = @"SubView/introCycle";
NSURL *fileURL = [[NSBundle mainBundle] URLForResource:loopPath withExtension:@"mp4"];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:fileURL options:nil];
NSString *tracksKey = @"tracks";
[asset loadValuesAsynchronouslyForKeys:@[tracksKey] completionHandler:^{
    // The completion block goes here.
    //NSLog(@"asset loaded asynchronously completed!");

    // Completion handler block.
    dispatch_async(dispatch_get_main_queue(),

                   ^{
                       NSError *error;
                       AVKeyValueStatus status = [asset statusOfValueForKey:tracksKey error:&error];

                       if (![self avPlayer] && status == AVKeyValueStatusLoaded) {
                           if(![self playerItem]){
                               self.playerItem = [AVPlayerItem playerItemWithAsset:asset];
                               [self.playerItem addObserver:self forKeyPath:@"status" options:0 context:AVMoviePlayerViewControllerStatusObservationContext];
                               self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
                               [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemDidReachEnd:)
                                                                     name:AVPlayerItemDidPlayToEndTimeNotification
                                                                     object:[self.avPlayer currentItem]];
                           }
                           self.avPlayer = [AVPlayer playerWithPlayerItem:self.playerItem];
                           [self.VideoLoopView setPlayer:self.avPlayer];
                       }

                       else {
                           // You should deal with the error appropriately.
                           NSLog(@"The asset's tracks were not loaded:\n%@", [error localizedDescription]);
                       }
                   });
}];

at the end of the loop the following method restarts the video

int countRounds = 0;
- (void)playerItemDidReachEnd:(NSNotification *)notification {
    *//block 1 - this is what we like*
    if (notification.object == self.playerItem) {

        countRounds++;

        [self.playerItem seekToTime:kCMTimeZero];
        [self.avPlayer play];

        }
    }
    *// block 2 - this we don't like*
    else {
        NSInteger reason = [[notification.userInfo objectForKey:AVPlayerItemDidPlayToEndTimeNotification] integerValue];
        NSLog(@"reason: %i", reason);
    }
    NSLog(@"playerItemDidReachEnd, starting round %i", countRounds);
}

which works fine. only block 1 is executed. log shows

MainView did load 
playerItemDidReachEnd, starting round 1 
playerItemDidReachEnd, starting round 2 
...

I then load another view and come back to the video loop view. when I leave the main view I do

- (void)viewDidDisappear:(BOOL)animated
{ 
    [[NSNotificationCenter defaultCenter]   removeObserver:self
                                            name:AVPlayerItemDidPlayToEndTimeNotification
                                            object:[self.avPlayer currentItem]];
    [self.avPlayer pause];
    self.avPlayer = nil;
    self.playerItem = nil;


    [super viewDidDisappear:animated];
    NSLog(@"MainView DidDisappear");
}

when I reload the mainview the video is reloaded and the loop is playing well. but now at the end of the loop both block 1 and block 2 are executed, logging

MainView did load (from old log above)
playerItemDidReachEnd, starting round 1 *(from old log above)*
playerItemDidReachEnd, starting round 2 *(from old log above)*
MainView DidDisappear   *(leaving the main view and loading a new one)*
MainView did load *(coming back to main view)*
reason: 0 *(block 2 is executed, triggered by what exactly? WHERE DOES THIS COME FROM?)*
playerItemDidReachEnd, starting round 2
playerItemDidReachEnd, starting round 3 *(this comes from block 1 - we like)*
reason: 0
playerItemDidReachEnd, starting round 3
playerItemDidReachEnd, starting round 4
reason: 0
playerItemDidReachEnd, starting round 4
playerItemDidReachEnd, starting round 5

so at the end of the loop the playerItemDidReachEnd-method is triggered twice. why is that? where does this come from?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T15:25:54+00:00Added an answer on June 10, 2026 at 3:25 pm

    You can avoid triggering from the wrong notificator by using

    if (notification.object == self.playerItem) ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently trying to implement a short video (10 seconds, looping) in a web
I saw this example on appsmuck.com for seamless video looping, it works fine on
I'm basically looping over a bunch of youtube video url's to get each video's
I am Playing the video in loop using set Looping(true) option and i will
I made a video player with a playlist. After around 45 min the sound
I am looking to display heightmaps from the video game Battlefield 2 as images
I'm looking into the feasibility of taking a video stream from a network device
I'm looking to dynamically add watermarks to a video on a website. What, in
I'm looking to learn some theory about data/audio/video compression, and would be glad to
I'm looking for a good jQuery plugin that allows HTML5 video playback, with graceful

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.