Explorar o código

add scrubbing support

Tobias Conradi %!s(int64=9) %!d(string=hai) anos
pai
achega
3b3174df24

+ 53 - 1
VLC for Apple TV/VLCFullscreenMovieTVViewController.m

@@ -53,6 +53,13 @@
     playpauseGesture.allowedPressTypes = @[@(UIPressTypePlayPause)];
     [self.view addGestureRecognizer:playpauseGesture];
 
+    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
+    [self.view addGestureRecognizer:panGestureRecognizer];
+
+    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectTap:)];
+    tapGestureRecognizer.allowedPressTypes = @[@(UIPressTypeSelect)];
+    [self.view addGestureRecognizer:tapGestureRecognizer];
+
 }
 
 #pragma mark - view events
@@ -102,12 +109,57 @@
 }
 
 #pragma mark - UIActions
-- (void) playPausePressed
+- (void)playPausePressed
 {
     VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
     [vpc playPause];
 }
 
+- (void)panGesture:(UIPanGestureRecognizer *)panGestureRecognizer
+{
+    VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
+    VLCTransportBar *bar = self.transportBar;
+
+    UIView *view = self.view;
+    CGPoint translation = [panGestureRecognizer translationInView:view];
+
+    if (!bar.scrubbing) {
+        if (translation.x > 100.0) {
+            bar.scrubbing = YES;
+            if (vpc.isPlaying) {
+                [vpc playPause];
+            }
+        } else {
+            return;
+        }
+    }
+
+    const CGFloat scaleFactor = 8.0;
+    CGFloat fractionInView = translation.x/CGRectGetWidth(view.bounds)/scaleFactor;
+    translation.x = 0.0;
+    [panGestureRecognizer setTranslation:translation inView:view];
+
+    CGFloat scrubbinFraction = MAX(0.0, MIN(bar.scrubbingFraction + fractionInView,1.0));
+    bar.scrubbingFraction = scrubbinFraction;
+    // MAX 1, _ is ugly hack to prevent --:-- instead of 00:00
+    int scrubbingTimeInt = MAX(1,vpc.mediaDuration*scrubbinFraction);
+    VLCTime *scrubbingTime = [VLCTime timeWithInt:scrubbingTimeInt];
+    bar.markerTimeLabel.text = [scrubbingTime stringValue];
+    VLCTime *remainingTime = [VLCTime timeWithInt:(int)vpc.mediaDuration-scrubbingTime.intValue];
+    bar.remainingTimeLabel.text = [remainingTime stringValue];
+}
+
+- (void)selectTap:(UITapGestureRecognizer *)recognizer
+{
+    VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
+    VLCTransportBar *bar = self.transportBar;
+    if (bar.scrubbing) {
+        bar.scrubbing = NO;
+        [vpc.mediaPlayer setPosition:bar.scrubbingFraction];
+        [vpc.mediaPlayer play];
+    }
+}
+
 #pragma mark - playback controller delegation
 
 - (void)prepareForMediaPlayback:(VLCPlaybackController *)controller

+ 10 - 7
VLC for Apple TV/VLCTransportBar.m

@@ -81,22 +81,25 @@ static inline void sharedSetup(VLCTransportBar *self) {
 }
 
 - (void)setBufferStartFraction:(CGFloat)bufferStartFraction {
-    _bufferStartFraction = bufferStartFraction;
-    self.bufferingBar.bufferStartFraction = bufferStartFraction;
+    CGFloat fraction = MAX(0.0, MIN(bufferStartFraction, 1.0));
+    _bufferStartFraction = fraction;
+    self.bufferingBar.bufferStartFraction = fraction;
 }
 - (void)setBufferEndFraction:(CGFloat)bufferEndFraction {
-    _bufferEndFraction = bufferEndFraction;
-    self.bufferingBar.bufferEndFraction = bufferEndFraction;
+    CGFloat fraction = MAX(0.0, MIN(bufferEndFraction, 1.0));
+    _bufferEndFraction = fraction;
+    self.bufferingBar.bufferEndFraction = fraction;
 }
 - (void)setPlaybackFraction:(CGFloat)playbackFraction {
-    _playbackFraction = playbackFraction;
+    CGFloat fraction = MAX(0.0, MIN(playbackFraction, 1.0));
+    _playbackFraction = fraction;
     if (!self.scrubbing) {
-        [self setScrubbingFraction:playbackFraction];
+        [self setScrubbingFraction:fraction];
     }
     [self setNeedsLayout];
 }
 - (void)setScrubbingFraction:(CGFloat)scrubbingFraction {
-    _scrubbingFraction = scrubbingFraction;
+    _scrubbingFraction = MAX(0.0, MIN(scrubbingFraction, 1.0));
     [self setNeedsLayout];
 }
 - (void)setScrubbing:(BOOL)scrubbing {