1

This is the code related to panning/ scrubbable video. The current issue occurs when the second swipe gesture has a delta from the last position of the first swipe. In other words this code needs to take into account the current time of the video to prevent the skip.

```

func didSwipe(panGR: UIPanGestureRecognizer) {

    let translation = panGR.translationInView(self.view)
    var horizontalTranslation =  Float(translation.x)

    let durationInSeconds = Float(CMTimeGetSeconds(self.playerView.player.player.currentItem!.asset.duration))

    // Using 275 as the limit for delta along x
    let translationLimit: Float = 275
    let minTranslation: Float = -1 * translationLimit
    let maxTranslation: Float = translationLimit

    if horizontalTranslation > maxTranslation {
        horizontalTranslation = maxTranslation
    }

    if horizontalTranslation < minTranslation {
        horizontalTranslation = minTranslation
    }


    let timeToSeekTo = normalize(horizontalTranslation , minDelta: minTranslation, maxDelta: maxTranslation, minDuration: 0, maxDuration: durationInSeconds)
    print("horizontal translation \(horizontalTranslation) \n timeToSeekTo: \(timeToSeekTo)")

    self.playerView.player.startScrubbing()
    self.playerView.player.scrub(timeToSeekTo)
    self.playerView.player.stopScrubbing()
}

func normalize(delta: Float, minDelta: Float, maxDelta: Float, minDuration: Float, maxDuration: Float) -> Float {

    let result = ((delta - minDelta) * (maxDuration - minDuration) / (maxDelta - minDelta) + minDuration)
    return result
}

```

I am setting the starting time to be exactly at half of the video length. This produces a good first swipe result in either direction. It has a noticeable skip on the second and subsequent swipes because it is not accounting for the current time of the video (i think).

  • FYI the normalize function was taken from James Bush's answer here http://stackoverflow.com/questions/26046946/pan-to-seek-avplayer/38770643?noredirect=1#comment65610546_38770643 – Walter Martin Vargas-Pena Sep 06 '16 at 16:43
  • I'm having the same issue as you were here and posted a question on SO: http://stackoverflow.com/questions/40363371/pan-gesture-with-avplayer. Could you please post the code here or in my question with the solution to this? That would be extremely helpful! Thank you for your time. – JEL Nov 02 '16 at 14:39

1 Answers1

2

Here's the code I wrote that employs the normalize function, and displays the current rate (and a Play icon that changes size according to the player rate:

- (IBAction)handlePanGesture:(UIPanGestureRecognizer *)sender {
        if (sender.state == UIGestureRecognizerStateBegan) {
            [self.rateLabel setHidden:FALSE];
            [self animatePlayerControl:@"▷" size:1.0];
            [UIView animateWithDuration:0.375 animations:^{
                (self.playerControlsLabel).alpha = 0.5f;
            }];
        } else if (sender.state == UIGestureRecognizerStateEnded) {
            [self.rateLabel setHidden:FALSE]; // set to TRUE after testing or remove hiding altogether
            //[self.delegate setRate:oldRate];
            //[self animatePlayerControl:@"□" size:1.0];
            //[_ChromaEpsilonGammaAppDelegate.playerViewController setRate:0.0];
            //[_ChromaEpsilonGammaAppDelegate.playerViewController stop];
            [UIView animateWithDuration:0.375 animations:^{
                (self.playerControlsLabel).alpha = 0.0f;
            }];
        } else if (sender.state == UIGestureRecognizerStateChanged){
            CGPoint location = [sender locationInView:self];
            float nlx = ((location.x / ((CGRectGetMidX(self.frame) / (self.frame.size.width / 2.0)))) / (self.frame.size.width / 2.0)) - 1.0;
            //float nly = ((location.y / ((CGRectGetMidY(self.view.frame) / (self.view.frame.size.width / 2.0)))) / (self.view.frame.size.width / 2.0)) - 1.0;
            nlx = nlx * 2.0;
            [self.delegate setRate:nlx];
            if (nlx > 0.0) [self animatePlayerControl:@"▷" size:nlx];
            else if (nlx < 0.0) [self animatePlayerControl:@"◁" size:fabs(nlx)];
            (self.rateLabel).text = [NSString stringWithFormat:@"%.2f", [self.delegate rate]];
        }
}

The label setup, which can go anywhere, not just in the drawRect method:

- (void)drawRect:(CGRect)rect {
    (self.brightnessLabel).textColor = [UIColor colorWithWhite:1.0 alpha:1.0];
    (self.brightnessLabel).font = [UIFont preferredFontForTextStyle:@"Apple Symbol"];
    (self.brightnessLabel).textAlignment = NSTextAlignmentCenter;
    (self.contrastLabel).textColor = [UIColor colorWithWhite:1.0 alpha:1.0];
    (self.contrastLabel).font = [UIFont preferredFontForTextStyle:@"Apple Symbol"];
    (self.contrastLabel).textAlignment = NSTextAlignmentCenter;
    _attrsDictionary = @{NSFontAttributeName: [UIFont systemFontOfSize:24.0 weight:UIFontWeightLight]};
}

And, the auto-adjusting Player icon size code:

- (void)animatePlayerControl:(NSString *)labelText size:(float)size {
    (self.playerControlsLabel).textColor = [UIColor colorWithWhite:1.0 alpha:1.0];
    (self.playerControlsLabel).font = [UIFont preferredFontForTextStyle:@"Apple Symbol"];
    (self.playerControlsLabel).textAlignment = NSTextAlignmentCenter;
    NSDictionary *attrsDictionary = @{NSFontAttributeName: [UIFont systemFontOfSize:fabs(48.0 * size) weight:UIFontWeightUltraLight]};
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:labelText attributes:attrsDictionary];
    (self.playerControlsLabel).attributedText = attrString;
}

The player icon is simply a character from the Apple Symbol font.

James Bush
  • 1,485
  • 14
  • 19
  • I'm having some trouble implementing this. Could you please check out my question here: http://stackoverflow.com/questions/40363371/pan-gesture-with-avplayer – JEL Nov 01 '16 at 15:59
  • @JEL Give me a call when you're ready to walk through it; this isn't hard by any means. Also, I can send you a single class file, which you can add to your project, and connect as a delegate via its protocol. I might post that file here, actually... – James Bush Nov 01 '16 at 17:38
  • Thanks so much for replying! We can go through it now. How would we do so - through StackOverflow chat? – JEL Nov 01 '16 at 17:41
  • Sure, send an e-mail to theoknock@gmail.com and I'll reply with the file; when you receive it, call or text me at (408) 685-4049 – James Bush Nov 01 '16 at 17:43