123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- /*****************************************************************************
- * VLC for iOS
- *****************************************************************************
- * Copyright (c) 2015 VideoLAN. All rights reserved.
- * $Id$
- *
- * Authors: Felix Paul Kühne <fkuehne # videolan.org>
- *
- * Refer to the COPYING file of the official project for license.
- *****************************************************************************/
- #import "VLCFullscreenMovieTVViewController.h"
- #import "VLCPlaybackInfoTVViewController.h"
- @interface VLCFullscreenMovieTVViewController (UIViewControllerTransitioningDelegate) <UIViewControllerTransitioningDelegate, UIGestureRecognizerDelegate>
- @end
- @interface VLCFullscreenMovieTVViewController ()
- {
- BOOL _playerIsSetup;
- BOOL _viewAppeared;
- }
- @end
- @implementation VLCFullscreenMovieTVViewController
- + (instancetype)fullscreenMovieTVViewController
- {
- return [[self alloc] initWithNibName:nil bundle:nil];
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.extendedLayoutIncludesOpaqueBars = YES;
- self.edgesForExtendedLayout = UIRectEdgeAll;
- NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
- [center addObserver:self
- selector:@selector(playbackDidStop:)
- name:VLCPlaybackControllerPlaybackDidStop
- object:nil];
- _movieView.userInteractionEnabled = NO;
- _playerIsSetup = NO;
- self.titleLabel.text = @"";
- self.transportBar.bufferStartFraction = 0.0;
- self.transportBar.bufferEndFraction = 1.0;
- self.transportBar.playbackFraction = 0.0;
- self.transportBar.scrubbingFraction = 0.0;
- self.dimmingView.alpha = 0.0;
- self.bottomOverlayView.hidden = YES;
- self.bufferingLabel.text = NSLocalizedString(@"PLEASE_WAIT", nil);
- // Panning and Swiping
- UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
- [self.view addGestureRecognizer:panGestureRecognizer];
- // Button presses
- UITapGestureRecognizer *playpauseGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(playPausePressed)];
- playpauseGesture.allowedPressTypes = @[@(UIPressTypePlayPause)];
- [self.view addGestureRecognizer:playpauseGesture];
- UITapGestureRecognizer *selectTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectButtonPressed:)];
- selectTapGestureRecognizer.allowedPressTypes = @[@(UIPressTypeSelect)];
- [self.view addGestureRecognizer:selectTapGestureRecognizer];
- UITapGestureRecognizer *menuTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(menuButtonPressed:)];
- menuTapGestureRecognizer.allowedPressTypes = @[@(UIPressTypeMenu)];
- menuTapGestureRecognizer.delegate = self;
- [self.view addGestureRecognizer:menuTapGestureRecognizer];
- UITapGestureRecognizer *upArrowRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showInfoVCIfNotScrubbing)];
- upArrowRecognizer.allowedPressTypes = @[@(UIPressTypeUpArrow)];
- [self.view addGestureRecognizer:upArrowRecognizer];
- }
- #pragma mark - view events
- - (void)viewWillAppear:(BOOL)animated
- {
- [super viewWillAppear:animated];
- [self.navigationController setNavigationBarHidden:YES animated:animated];
- VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
- vpc.delegate = self;
- [vpc recoverPlaybackState];
- }
- - (void)viewDidAppear:(BOOL)animated
- {
- [super viewDidAppear:animated];
- _viewAppeared = YES;
- VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
- [vpc recoverDisplayedMetadata];
- vpc.videoOutputView = nil;
- vpc.videoOutputView = self.movieView;
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- - (void)viewWillDisappear:(BOOL)animated
- {
- VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
- if (vpc.videoOutputView == self.movieView) {
- vpc.videoOutputView = nil;
- }
- _viewAppeared = NO;
- [self.navigationController setNavigationBarHidden:NO animated:YES];
- [vpc stopPlayback];
- [super viewWillDisappear:animated];
- }
- - (BOOL)canBecomeFirstResponder
- {
- return YES;
- }
- #pragma mark - UIActions
- - (void)playPausePressed
- {
- VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
- [vpc playPause];
- }
- - (void)panGesture:(UIPanGestureRecognizer *)panGestureRecognizer
- {
- VLCTransportBar *bar = self.transportBar;
- UIView *view = self.view;
- CGPoint translation = [panGestureRecognizer translationInView:view];
- if (!bar.scrubbing) {
- if (ABS(translation.x) > 150.0) {
- [self startScrubbing];
- } else if (translation.y > 200.0) {
- [self showInfoVCIfNotScrubbing];
- return;
- } 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 scrubbingFraction = MAX(0.0, MIN(bar.scrubbingFraction + fractionInView,1.0));
- bar.scrubbingFraction = scrubbingFraction;
- [self updateTimeLabelsForScrubbingFraction:scrubbingFraction];
- }
- - (void)selectButtonPressed:(UITapGestureRecognizer *)recognizer
- {
- VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
- VLCTransportBar *bar = self.transportBar;
- if (bar.scrubbing) {
- bar.playbackFraction = bar.scrubbingFraction;
- [vpc.mediaPlayer setPosition:bar.scrubbingFraction];
- [self stopScrubbing];
- }
- }
- - (void)menuButtonPressed:(UITapGestureRecognizer *)recognizer
- {
- VLCTransportBar *bar = self.transportBar;
- if (bar.scrubbing) {
- [UIView animateWithDuration:0.3 animations:^{
- bar.scrubbingFraction = bar.playbackFraction;
- [bar layoutIfNeeded];
- }];
- [self updateTimeLabelsForScrubbingFraction:bar.playbackFraction];
- [self stopScrubbing];
- }
- }
- - (void)showInfoVCIfNotScrubbing
- {
- if (self.transportBar.scrubbing) {
- return;
- }
- VLCPlaybackInfoTVViewController *infoController = [[VLCPlaybackInfoTVViewController alloc] initWithNibName:nil bundle:nil];
- infoController.transitioningDelegate = self;
- infoController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
- // TODO: configure with player info
- [self presentViewController:infoController animated:YES completion:nil];
- }
- #pragma mark -
- - (void)updateTimeLabelsForScrubbingFraction:(CGFloat)scrubbingFraction
- {
- VLCTransportBar *bar = self.transportBar;
- VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
- // MAX 1, _ is ugly hack to prevent --:-- instead of 00:00
- int scrubbingTimeInt = MAX(1,vpc.mediaDuration*scrubbingFraction);
- 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)startScrubbing
- {
- VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
- self.transportBar.scrubbing = YES;
- [self updateDimmingView];
- if (vpc.isPlaying) {
- [vpc playPause];
- }
- }
- - (void)stopScrubbing
- {
- self.transportBar.scrubbing = NO;
- [self updateDimmingView];
- VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
- [vpc.mediaPlayer play];
- }
- - (void)updateDimmingView
- {
- BOOL shouldBeVisible = self.transportBar.scrubbing;
- BOOL isVisible = self.dimmingView.alpha == 1.0;
- if (shouldBeVisible != isVisible) {
- [UIView animateWithDuration:0.3 animations:^{
- self.dimmingView.alpha = shouldBeVisible ? 1.0 : 0.0;
- }];
- }
- }
- - (void)updateActivityIndicatorForState:(VLCMediaPlayerState)state {
- UIActivityIndicatorView *indicator = self.activityIndicator;
- switch (state) {
- case VLCMediaPlayerStateBuffering:
- if (!indicator.isAnimating) {
- self.activityIndicator.alpha = 1.0;
- [self.activityIndicator startAnimating];
- }
- break;
- default:
- if (indicator.isAnimating) {
- [self.activityIndicator stopAnimating];
- self.activityIndicator.alpha = 0.0;
- }
- break;
- }
- }
- #pragma mark - playback controller delegation
- - (void)prepareForMediaPlayback:(VLCPlaybackController *)controller
- {
- APLog(@"%s", __PRETTY_FUNCTION__);
- }
- - (void)playbackDidStop:(NSNotification *)aNotification
- {
- [self dismissViewControllerAnimated:YES completion:nil];
- }
- - (void)mediaPlayerStateChanged:(VLCMediaPlayerState)currentState
- isPlaying:(BOOL)isPlaying
- currentMediaHasTrackToChooseFrom:(BOOL)currentMediaHasTrackToChooseFrom
- currentMediaHasChapters:(BOOL)currentMediaHasChapters
- forPlaybackController:(VLCPlaybackController *)controller
- {
- [self updateActivityIndicatorForState:currentState];
- if (controller.isPlaying && !self.bufferingLabel.hidden) {
- [UIView animateWithDuration:.3 animations:^{
- self.bufferingLabel.hidden = YES;
- self.bottomOverlayView.hidden = NO;
- }];
- }
- }
- - (void)displayMetadataForPlaybackController:(VLCPlaybackController *)controller
- title:(NSString *)title
- artwork:(UIImage *)artwork
- artist:(NSString *)artist
- album:(NSString *)album
- audioOnly:(BOOL)audioOnly
- {
- self.titleLabel.text = title;
- }
- - (void)playbackPositionUpdated:(VLCPlaybackController *)controller
- {
- VLCMediaPlayer *mediaPlayer = [VLCPlaybackController sharedInstance].mediaPlayer;
- // FIXME: hard coded state since the state in mediaPlayer is incorrectly still buffering
- [self updateActivityIndicatorForState:VLCMediaPlayerStatePlaying];
- VLCTransportBar *transportBar = self.transportBar;
- transportBar.remainingTimeLabel.text = [[mediaPlayer remainingTime] stringValue];
- transportBar.markerTimeLabel.text = [[mediaPlayer time] stringValue];
- transportBar.playbackFraction = mediaPlayer.position;
- }
- #pragma mark - gesture recognizer delegate
- - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
- if ([gestureRecognizer.allowedPressTypes containsObject:@(UIPressTypeMenu)]) {
- return self.transportBar.scrubbing;
- }
- return YES;
- }
- @end
- @implementation VLCFullscreenMovieTVViewController (UIViewControllerTransitioningDelegate)
- - (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
- {
- return [[VLCPlaybackInfoTVTransitioningAnimator alloc] init];
- }
- - (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
- {
- return [[VLCPlaybackInfoTVTransitioningAnimator alloc] init];
- }
- @end
|