Simon
Simon

Reputation: 394

AVPlayer play for exact amount of time

Using an AVPlayer I would like to play a mov file for exactly 1 second then pause it.

Currently I'm playing the mov then setting a timer to pause it after 1 second as below. Unfortunately, this does not appear to be exactly accurate and the mov is sometimes playing for slightly shorter or longer than 1 second. Is there a more accurate way of doing this please?

[self.player4 play];
[self performSelector:@selector(pausePlayer4:) withObject:nil afterDelay:1.0];

- (void)pausePlayer4:(NSTimer *)timer
{
    [self.player4 pause];
}

Upvotes: 1

Views: 596

Answers (2)

damian
damian

Reputation: 3674

Even if you can get an event to fire precisely enough, media playback on iOS devices happens in an entirely different process (a daemon) and there's always latency when doing IPC.

Depending on your needs it might be best to build an AVMutableComposition that plays exactly one second of content from your AVURLAsset, and then assign the composition to your player.

Upvotes: 1

amergin
amergin

Reputation: 3176

The best way wold be to add a boundary observer to trigger after a second

    NSValue *endBoundary = [NSValue valueWithCMTime:CMTimeMakeWithSeconds(1.0, 300)];

    [self.player4 addBoundaryTimeObserverForTimes:@[endBoundary]
                                            queue:NULL
                                       usingBlock:^{
                                        [self.player4 stop];
                                        }];

    [self.player4 play];

Upvotes: 0

Related Questions