Moti Dhan
Moti Dhan

Reputation: 1

iOS AVPlayer not able to pause after rewind/fastforward

Scenario

Issue

I am unable to pause the video after seekToTime: is called.

Question

How to pause the player after calling seekToTime?

I have tried both [Avplayer pause] as well as setting Avplayer.rate to 0. Nothing stops the video after calling seekToTime.

My code

@property (strong, nonatomic) AVPlayer *player;
.
.
.
@synthesize player
.
.
.
- (IBAction)rewind:(id)sender {
    NSLog(@"rewind called");

    int secToRewind = DEFAULT_REWIND_SEC;
    int time = CMTimeGetSeconds(player.currentTime) - secToRewind;
    CMTime newTime = CMTimeMakeWithSeconds(time, 1);
    CMTime tolerance = CMTimeMakeWithSeconds(1, 1);

    __weak __typeof__(self) weakSelf = self;
    int playerRate = player.rate;
    [player seekToTime:newTime toleranceBefore: tolerance toleranceAfter: tolerance completionHandler:^(BOOL finished) {

        NSLog(@"seekToTime called");

        __strong __typeof__(self) strongSelf = weakSelf;
        if (playerRate == 0) {
            [player pause];
        }

    }];


}

Upvotes: 0

Views: 169

Answers (2)

Moti Dhan
Moti Dhan

Reputation: 1

I found the code causing the autoplay. There was an observer attached to the player. Whenever the player item is in AVPlayerItemStatusReadyToPlay, the player was set to play that item regardless of play/pause state.

Code with fixes

case AVPlayerItemStatusReadyToPlay: {

            if ([playButton isSelected]) {
                 [self.player play];
            }


        }

Upvotes: 0

CTABUYO
CTABUYO

Reputation: 712

The problem I'd guess is within the buffering of the content, since you are going X (15 supposedly) seconds back, that's a lot and it needs a little bit of time to buffer the content.

Try the following code...

@property (strong, nonatomic) AVPlayer *player;
.
.
.
@synthesize player
.
.
.
- (IBAction)rewind:(id)sender {
    NSLog(@"rewind called");

    int secToRewind = DEFAULT_REWIND_SEC;
    int time = CMTimeGetSeconds(player.currentTime) - secToRewind;
    CMTime newTime = CMTimeMakeWithSeconds(time, 1);
    CMTime tolerance = CMTimeMakeWithSeconds(1, 1);

    __weak __typeof__(self) weakSelf = self;
    int playerRate = player.rate;
    [player seekToTime:newTime toleranceBefore: tolerance toleranceAfter: tolerance completionHandler:^(BOOL finished) {



        do {

          NSLog(@"Successful rewind");

         __strong __typeof__(self) strongSelf = weakSelf;
         if (playerRate == 0) {
             [player playAt:[player currentTime]];
         }

       } while (completed == true);

    }];


}

Upvotes: 0

Related Questions