123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- /*****************************************************************************
- * VLCMiniPlaybackView.m
- * VLC for iOS
- *****************************************************************************
- * Copyright (c) 2015 VideoLAN. All rights reserved.
- * $Id$
- *
- * Author: Felix Paul Kühne <fkuehne # videolan.org>
- *
- * Refer to the COPYING file of the official project for license.
- *****************************************************************************/
- #import "VLCMiniPlaybackView.h"
- #import "VLCPlaybackController.h"
- #import "VLCAppDelegate.h"
- #import "VLCPlaylistViewController.h"
- #import "VLCPlayerDisplayController.h"
- #import "VLCKeychainCoordinator.h"
- @interface VLCMiniPlaybackView () <VLCPlaybackControllerDelegate, UIGestureRecognizerDelegate>
- {
- UIImageView *_artworkView;
- UIView *_videoView;
- UIButton *_previousButton;
- UIButton *_playPauseButton;
- UIButton *_nextButton;
- UIButton *_expandButton;
- UILabel *_metaDataLabel;
- UITapGestureRecognizer *_labelTapRecognizer;
- UITapGestureRecognizer *_artworkTapRecognizer;
- }
- @property (nonatomic, weak) VLCPlaybackController *playbackController;
- @end
- @implementation VLCMiniPlaybackView
- - (void)dealloc
- {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- - (instancetype)initWithFrame:(CGRect)viewFrame
- {
- self = [super initWithFrame:viewFrame];
- if (!self)
- return self;
- CGRect previousRect;
- CGFloat buttonSize = 44.;
- _artworkView = [[UIImageView alloc] initWithFrame:CGRectMake(0., 0., 60., 60.)];
- _artworkView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
- _artworkView.backgroundColor = [UIColor VLCDarkBackgroundColor];
- _artworkView.opaque = YES;
- [self addSubview:_artworkView];
- /* build buttons from right to left */
- _expandButton = [UIButton buttonWithType:UIButtonTypeCustom];
- [_expandButton setImage:[UIImage imageNamed:@"ratioIcon"] forState:UIControlStateNormal];
- _expandButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
- [_expandButton addTarget:self action:@selector(pushFullPlaybackView:) forControlEvents:UIControlEventTouchUpInside];
- _expandButton.frame = previousRect = CGRectMake(viewFrame.size.width - buttonSize, (viewFrame.size.height - buttonSize) / 2., buttonSize, buttonSize);
- _expandButton.accessibilityLabel = NSLocalizedString(@"FULLSCREEN_PLAYBACK", nil);
- [self addSubview:_expandButton];
- _nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
- [_nextButton setImage:[UIImage imageNamed:@"forwardIcon"] forState:UIControlStateNormal];
- [_nextButton sizeToFit];
- _nextButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
- [_nextButton addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];
- _nextButton.frame = previousRect = CGRectMake(previousRect.origin.x - buttonSize, (viewFrame.size.height - buttonSize) / 2., buttonSize, buttonSize);
- _nextButton.accessibilityLabel = NSLocalizedString(@"FWD_BUTTON", nil);
- [self addSubview:_nextButton];
- _playPauseButton = [UIButton buttonWithType:UIButtonTypeCustom];
- [_playPauseButton setImage:[UIImage imageNamed:@"playIcon"] forState:UIControlStateNormal];
- [_playPauseButton sizeToFit];
- _playPauseButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
- [_playPauseButton addTarget:self action:@selector(playPauseAction:) forControlEvents:UIControlEventTouchUpInside];
- _playPauseButton.accessibilityLabel = NSLocalizedString(@"PLAY_PAUSE_BUTTON", nil);
- _playPauseButton.accessibilityHint = NSLocalizedString(@"LONGPRESS_TO_STOP", nil);
- _playPauseButton.isAccessibilityElement = YES;
- _playPauseButton.frame = previousRect = CGRectMake(previousRect.origin.x - buttonSize, (viewFrame.size.height - buttonSize) / 2., buttonSize, buttonSize);
- UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(playPauseLongPress:)];
- [_playPauseButton addGestureRecognizer:longPressRecognizer];
- [self addSubview:_playPauseButton];
- _previousButton = [UIButton buttonWithType:UIButtonTypeCustom];
- [_previousButton setImage:[UIImage imageNamed:@"backIcon"] forState:UIControlStateNormal];
- [_previousButton sizeToFit];
- _previousButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
- [_previousButton addTarget:self action:@selector(previousAction:) forControlEvents:UIControlEventTouchUpInside];
- _previousButton.frame = previousRect = CGRectMake(previousRect.origin.x - buttonSize, (viewFrame.size.height - buttonSize) / 2., buttonSize, buttonSize);
- _previousButton.accessibilityLabel = NSLocalizedString(@"BWD_BUTTON", nil);
- [self addSubview:_previousButton];
- CGFloat artworkViewWidth = _artworkView.frame.size.width;
- _metaDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(artworkViewWidth + 10., 0., previousRect.origin.x - artworkViewWidth - 10., viewFrame.size.height)];
- _metaDataLabel.font = [UIFont systemFontOfSize:12.];
- _metaDataLabel.textColor = [UIColor VLCLightTextColor];
- _metaDataLabel.numberOfLines = 0;
- _metaDataLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
- [self addSubview:_metaDataLabel];
- _labelTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized)];
- _labelTapRecognizer.delegate = self;
- _labelTapRecognizer.numberOfTouchesRequired = 1;
- [_metaDataLabel addGestureRecognizer:_labelTapRecognizer];
- _metaDataLabel.userInteractionEnabled = YES;
- _artworkTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized)];
- _artworkTapRecognizer.delegate = self;
- _artworkTapRecognizer.numberOfTouchesRequired = 1;
- [_artworkView addGestureRecognizer:_artworkTapRecognizer];
- _artworkView.userInteractionEnabled = YES;
- NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
- [center addObserver:self
- selector:@selector(appBecameActive:)
- name:UIApplicationDidBecomeActiveNotification
- object:nil];
- [center addObserver:self
- selector:@selector(appBecameActive:)
- name:VLCPasscodeValidated
- object:nil];
- return self;
- }
- - (void)appBecameActive:(NSNotification *)aNotification
- {
- VLCPlayerDisplayController *pdc = [(VLCAppDelegate *)[UIApplication sharedApplication].delegate playerDisplayController];
- if (pdc.displayMode == VLCPlayerDisplayControllerDisplayModeMiniplayer) {
- VLCPlaybackController *vpc = self.playbackController;
- [vpc recoverDisplayedMetadata];
- }
- }
- - (void)tapRecognized
- {
- [self pushFullPlaybackView:nil];
- }
- - (void)previousAction:(id)sender
- {
- [self.playbackController backward];
- }
- - (void)playPauseAction:(id)sender
- {
- [self.playbackController playPause];
- }
- - (void)playPauseLongPress:(UILongPressGestureRecognizer *)recognizer
- {
- switch (recognizer.state) {
- case UIGestureRecognizerStateBegan:
- [_playPauseButton setImage:[UIImage imageNamed:@"stopIcon"] forState:UIControlStateNormal];
- break;
- case UIGestureRecognizerStateEnded:
- [self.playbackController stopPlayback];
- break;
- case UIGestureRecognizerStateCancelled:
- case UIGestureRecognizerStateFailed:
- [self updatePlayPauseButton];
- break;
- default:
- break;
- }
- }
- - (void)nextAction:(id)sender
- {
- [self.playbackController forward];
- }
- - (void)pushFullPlaybackView:(id)sender
- {
- [[UIApplication sharedApplication] sendAction:@selector(showFullscreenPlayback) to:nil from:self forEvent:nil];
- }
- - (void)updatePlayPauseButton
- {
- const BOOL isPlaying = self.playbackController.isPlaying;
- UIImage *playPauseImage = isPlaying ? [UIImage imageNamed:@"pauseIcon"] : [UIImage imageNamed:@"playIcon"];
- [_playPauseButton setImage:playPauseImage forState:UIControlStateNormal];
- }
- - (void)setupForWork:(VLCPlaybackController *)playbackController
- {
- self.playbackController = playbackController;
- [self updatePlayPauseButton];
- playbackController.delegate = self;
- [playbackController recoverDisplayedMetadata];
- }
- - (void)mediaPlayerStateChanged:(VLCMediaPlayerState)currentState
- isPlaying:(BOOL)isPlaying
- currentMediaHasTrackToChooseFrom:(BOOL)currentMediaHasTrackToChooseFrom
- currentMediaHasChapters:(BOOL)currentMediaHasChapters
- forPlaybackController:(VLCPlaybackController *)controller
- {
- [self updatePlayPauseButton];
- }
- - (void)displayMetadataForPlaybackController:(VLCPlaybackController *)controller
- title:(NSString *)title
- artwork:(UIImage *)artwork
- artist:(NSString *)artist
- album:(NSString *)album
- audioOnly:(BOOL)audioOnly
- {
- if (audioOnly) {
- _artworkView.contentMode = UIViewContentModeScaleAspectFill;
- _artworkView.image = artwork ? artwork : [UIImage imageNamed:@"no-artwork"];
- if (_videoView) {
- [_videoView removeFromSuperview];
- _videoView = nil;
- }
- [_artworkView addGestureRecognizer:_artworkTapRecognizer];
- } else {
- _artworkView.image = nil;
- if (_videoView) {
- [_videoView removeFromSuperview];
- _videoView = nil;
- }
- VLCPlayerDisplayController *pdc = [(VLCAppDelegate *)[UIApplication sharedApplication].delegate playerDisplayController];
- if (pdc.displayMode == VLCPlayerDisplayControllerDisplayModeMiniplayer) {
- _videoView = [[UIView alloc] initWithFrame:_artworkView.frame];
- [_videoView setClipsToBounds:YES];
- [_videoView addGestureRecognizer:_artworkTapRecognizer];
- _videoView.userInteractionEnabled = YES;
- [self addSubview:_videoView];
- controller.videoOutputView = _videoView;
- }
- }
- NSString *metaDataString;
- if (artist)
- metaDataString = artist;
- if (album)
- metaDataString = [metaDataString stringByAppendingFormat:@" — %@", album];
- if (metaDataString)
- metaDataString = [metaDataString stringByAppendingFormat:@"\n%@", title];
- else
- metaDataString = title;
- _metaDataLabel.text = metaDataString;
- }
- @end
|