Created
March 28, 2017 09:18
-
-
Save frosty/1d37e066224dc4b99551bcf4a1d29a49 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/Pod/Classes/WPVideoPlayerView.m b/Pod/Classes/WPVideoPlayerView.m | |
index 6da5424..4ed3fa2 100644 | |
--- a/Pod/Classes/WPVideoPlayerView.m | |
+++ b/Pod/Classes/WPVideoPlayerView.m | |
@@ -12,6 +12,7 @@ @interface WPVideoPlayerView() | |
@property (nonatomic, strong) AVPlayerItem *playerItem; | |
@property (nonatomic, strong) UIToolbar *controlToolbar; | |
@property (nonatomic, strong) UIBarButtonItem * videoDurationButton; | |
+@property (nonatomic, strong) UILabel * videoDurationLabel; | |
@property (nonatomic, strong) id timeObserver; | |
@end | |
@@ -78,23 +79,40 @@ - (UIToolbar *)controlToolbar { | |
} | |
_controlToolbar = [[UIToolbar alloc] init]; | |
_controlToolbar.hidden = YES; | |
+ _controlToolbar.tintColor = [UIColor whiteColor]; | |
+ _controlToolbar.barStyle = UIBarStyleBlack; | |
+ _controlToolbar.translucent = YES; | |
+ | |
[self updateControlToolbar]; | |
return _controlToolbar; | |
} | |
- (UIBarButtonItem *)videoDurationButton { | |
if (_videoDurationButton == nil) { | |
- _videoDurationButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil]; | |
- NSDictionary *titleAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]}; | |
- [_videoDurationButton setTitleTextAttributes:titleAttributes forState:UIControlStateNormal]; | |
- [_videoDurationButton setTitleTextAttributes:titleAttributes forState:UIControlStateSelected]; | |
- [_videoDurationButton setTitleTextAttributes:titleAttributes forState:UIControlStateHighlighted]; | |
- [_videoDurationButton setTitleTextAttributes:titleAttributes forState:UIControlStateDisabled]; | |
+ _videoDurationButton = [[UIBarButtonItem alloc] initWithCustomView:self.videoDurationLabel]; | |
_videoDurationButton.enabled = NO; | |
} | |
return _videoDurationButton; | |
} | |
+- (UILabel *)videoDurationLabel { | |
+ if (_videoDurationLabel == nil) { | |
+ _videoDurationLabel = [UILabel new]; | |
+ _videoDurationLabel.textColor = [UIColor whiteColor]; | |
+ _videoDurationLabel.font = [UIFont boldSystemFontOfSize:14.0]; | |
+ _videoDurationLabel.adjustsFontSizeToFitWidth = NO; | |
+ _videoDurationLabel.textAlignment = NSTextAlignmentRight; | |
+ _videoDurationLabel.lineBreakMode = NSLineBreakByTruncatingTail; | |
+ | |
+ // Fix the label to the widest size we want to show, so it doesn't | |
+ // resize itself and move around as we update the content | |
+ _videoDurationLabel.text = @"0:00:00 / 0:00:00"; | |
+ [_videoDurationLabel sizeToFit]; | |
+ } | |
+ | |
+ return _videoDurationLabel; | |
+} | |
+ | |
- (void)setVideoURL:(NSURL *)videoURL { | |
_videoURL = videoURL; | |
AVURLAsset *asset = [AVURLAsset assetWithURL:videoURL]; | |
@@ -183,7 +201,11 @@ - (BOOL)controlToolbarHidden { | |
- (void)updateControlToolbar { | |
UIBarButtonSystemItem playPauseButton = [self.player timeControlStatus] == AVPlayerTimeControlStatusPaused ? UIBarButtonSystemItemPlay : UIBarButtonSystemItemPause; | |
+ UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; | |
+ fixedSpace.width = self.videoDurationLabel.bounds.size.width; | |
+ | |
self.controlToolbar.items = @[ | |
+ fixedSpace, | |
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], | |
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:playPauseButton target:self action:@selector(togglePlayPause)], | |
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], | |
@@ -200,7 +222,7 @@ - (void)updateVideoDuration { | |
double currentSeconds = CMTimeGetSeconds(playerItem.currentTime); | |
NSString *totalDuration = [self stringFromTimeInterval:totalSeconds]; | |
NSString *currentDuration = [self stringFromTimeInterval:currentSeconds]; | |
- self.videoDurationButton.title = [NSString stringWithFormat:@"%@/%@", currentDuration, totalDuration]; | |
+ self.videoDurationLabel.text = [NSString stringWithFormat:@"%@ / %@", currentDuration, totalDuration]; | |
} | |
- (NSString *)stringFromTimeInterval:(NSTimeInterval)timeInterval |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment