VLCMiniPlaybackView.m 9.99 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*****************************************************************************
 * 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"
17
#import "VLCPlayerDisplayController.h"
18

19
@interface VLCMiniPlaybackView () <VLCPlaybackControllerDelegate, UIGestureRecognizerDelegate>
20
21
22
23
24
25
26
27
{
    UIImageView *_artworkView;
    UIView *_videoView;
    UIButton *_previousButton;
    UIButton *_playPauseButton;
    UIButton *_nextButton;
    UIButton *_expandButton;
    UILabel *_metaDataLabel;
28
29
    UITapGestureRecognizer *_labelTapRecognizer;
    UITapGestureRecognizer *_artworkTapRecognizer;
30
}
31
@property (nonatomic, weak) VLCPlaybackController *playbackController;
32
33
34
35
36

@end

@implementation VLCMiniPlaybackView

37
38
39
40
41
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

42
43
44
45
46
47
48
- (instancetype)initWithFrame:(CGRect)viewFrame
{
    self = [super initWithFrame:viewFrame];
    if (!self)
        return self;

    CGRect previousRect;
49
    CGFloat buttonSize = 44.;
50
51
52

    _artworkView = [[UIImageView alloc] initWithFrame:CGRectMake(0., 0., 60., 60.)];
    _artworkView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
53
54
    _artworkView.backgroundColor = [UIColor VLCDarkBackgroundColor];
    _artworkView.opaque = YES;
55
56
57
58
59
60
61
    [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];
62
    _expandButton.frame = previousRect = CGRectMake(viewFrame.size.width - buttonSize, (viewFrame.size.height - buttonSize) / 2., buttonSize, buttonSize);
63
64
65
66
67
68
69
    [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];
70
    _nextButton.frame = previousRect = CGRectMake(previousRect.origin.x - buttonSize, (viewFrame.size.height - buttonSize) / 2., buttonSize, buttonSize);
71
72
73
74
75
76
77
    [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];
78
79
80
    _playPauseButton.accessibilityLabel = NSLocalizedString(@"PLAY_PAUSE_BUTTON", nil);
    _playPauseButton.accessibilityHint = NSLocalizedString(@"LONGPRESS_TO_STOP", nil);
    _playPauseButton.isAccessibilityElement = YES;
81
    _playPauseButton.frame = previousRect = CGRectMake(previousRect.origin.x - buttonSize, (viewFrame.size.height - buttonSize) / 2., buttonSize, buttonSize);
82
83
    UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(playPauseLongPress:)];
    [_playPauseButton addGestureRecognizer:longPressRecognizer];
84
85
86
87
88
89
    [self addSubview:_playPauseButton];

    _previousButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_previousButton setImage:[UIImage imageNamed:@"backIcon"] forState:UIControlStateNormal];
    [_previousButton sizeToFit];
    _previousButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
90
91
    [_previousButton addTarget:self action:@selector(previousAction:) forControlEvents:UIControlEventTouchUpInside];
    _previousButton.frame = previousRect = CGRectMake(previousRect.origin.x - buttonSize, (viewFrame.size.height - buttonSize) / 2., buttonSize, buttonSize);
92
93
94
    [self addSubview:_previousButton];

    CGFloat artworkViewWidth = _artworkView.frame.size.width;
95
    _metaDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(artworkViewWidth + 10., 0., previousRect.origin.x - artworkViewWidth - 10., viewFrame.size.height)];
96
97
98
    _metaDataLabel.font = [UIFont systemFontOfSize:12.];
    _metaDataLabel.textColor = [UIColor VLCLightTextColor];
    _metaDataLabel.numberOfLines = 0;
99
    _metaDataLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
100
101
    [self addSubview:_metaDataLabel];

102
103
104
105
106
107
108
109
110
111
112
113
    _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;

114
115
116
117
118
119
120
121
122
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self
               selector:@selector(appBecameActive:)
                   name:UIApplicationDidBecomeActiveNotification
                 object:nil];
    [center addObserver:self
               selector:@selector(appBecameActive:)
                   name:VLCPasscodeValidated
                 object:nil];
123

124
125
126
    return self;
}

127
128
129
130
131
132
133
134
135
- (void)appBecameActive:(NSNotification *)aNotification
{
    VLCPlayerDisplayController *pdc = [(VLCAppDelegate *)[UIApplication sharedApplication].delegate playerDisplayController];
    if (pdc.displayMode == VLCPlayerDisplayControllerDisplayModeMiniplayer) {
        VLCPlaybackController *vpc = self.playbackController;
        [vpc recoverDisplayedMetadata];
    }
}

136
137
138
139
140
- (void)tapRecognized
{
    [self pushFullPlaybackView:nil];
}

141
142
- (void)previousAction:(id)sender
{
143
    [self.playbackController backward];
144
145
146
147
}

- (void)playPauseAction:(id)sender
{
148
    [self.playbackController playPause];
149
150
}

151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
- (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;
    }
}

169
170
- (void)nextAction:(id)sender
{
171
    [self.playbackController forward];
172
173
174
175
}

- (void)pushFullPlaybackView:(id)sender
{
176
    [[UIApplication sharedApplication] sendAction:@selector(showFullscreenPlayback) to:nil from:self forEvent:nil];
177
178
}

179
180
181
182
183
184
185
186

- (void)updatePlayPauseButton
{
    const BOOL isPlaying = self.playbackController.isPlaying;
    UIImage *playPauseImage = isPlaying ? [UIImage imageNamed:@"pauseIcon"] : [UIImage imageNamed:@"playIcon"];
    [_playPauseButton setImage:playPauseImage forState:UIControlStateNormal];
}

187
- (void)setupForWork:(VLCPlaybackController *)playbackController
188
{
189
    self.playbackController = playbackController;
190
    [self updatePlayPauseButton];
191
192
193
194
195
196
197
198
199
200
    playbackController.delegate = self;
    [playbackController recoverDisplayedMetadata];
}

- (void)mediaPlayerStateChanged:(VLCMediaPlayerState)currentState
                      isPlaying:(BOOL)isPlaying
currentMediaHasTrackToChooseFrom:(BOOL)currentMediaHasTrackToChooseFrom
        currentMediaHasChapters:(BOOL)currentMediaHasChapters
          forPlaybackController:(VLCPlaybackController *)controller
{
201
    [self updatePlayPauseButton];
202
203
204
205
206
207
208
209
210
211
}

- (void)displayMetadataForPlaybackController:(VLCPlaybackController *)controller
                                       title:(NSString *)title
                                     artwork:(UIImage *)artwork
                                      artist:(NSString *)artist
                                       album:(NSString *)album
                                   audioOnly:(BOOL)audioOnly
{
    if (audioOnly) {
Felix Paul Kühne's avatar
Felix Paul Kühne committed
212
213
        _artworkView.contentMode = UIViewContentModeScaleAspectFill;
        _artworkView.image = artwork ? artwork : [UIImage imageNamed:@"no-artwork"];
214
215
216
217
        if (_videoView) {
            [_videoView removeFromSuperview];
            _videoView = nil;
        }
218
        [_artworkView addGestureRecognizer:_artworkTapRecognizer];
219
    } else {
220
        _artworkView.image = nil;
221
222
223
224
        if (_videoView) {
            [_videoView removeFromSuperview];
            _videoView = nil;
        }
225
226
227
228
229
230
231
232
233
234

        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;
        }
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
    }

    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