Skip to content

Commit

Permalink
device volume control
Browse files Browse the repository at this point in the history
  • Loading branch information
priore committed May 4, 2016
1 parent ebbd728 commit 952cdce
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 34 deletions.
2 changes: 1 addition & 1 deletion AVPlayerOverlay.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'AVPlayerOverlay'
s.version = '1.2.1'
s.version = '1.2.2'
s.summary = 'AVPlayer with custom controls and full screen features.'
s.license = 'MIT'
s.ios.platform = '7.1'
Expand Down
88 changes: 55 additions & 33 deletions AVPlayerOverlay/AVPlayer/AVPlayerOverlayVC.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,28 @@
//
#define PLAYERBAR_AUTOHIDE 5.0

#import <AVFoundation/AVFoundation.h>
#import "AVPlayerOverlayVC.h"

@import AVFoundation;
@import MediaPlayer;

@interface AVPlayerOverlayVC ()
{
id timeObserver;
BOOL isVideoSliderMoving;
}

@property (nonatomic, weak) UIView *containerView;
@property (nonatomic, weak) UIWindow *mainWindow;
@property (nonatomic, weak) UIViewController *mainParent;
@property (nonatomic, weak) UISlider *mpSlider;

@property (nonatomic, assign) BOOL statusBarHidden;
@property (nonatomic, assign) BOOL navbarHidden;
@property (nonatomic, assign) CGRect currentFrame;

@property (nonatomic, strong) UIWindow *window;
@property (nonatomic, strong) MPVolumeView *volume;

@end

Expand All @@ -28,8 +40,18 @@ - (void)viewDidLoad
self.view.clipsToBounds = YES;
self.view.backgroundColor = [UIColor clearColor];

// system volume
self.volume = [[MPVolumeView alloc] initWithFrame:CGRectZero];
for (id view in _volume.subviews) {
if ([view isKindOfClass:[UISlider class]]) {
_mpSlider = view;
break;
}
}

_volumeSlider.hidden = YES;
_volumeSlider.transform = CGAffineTransformMakeRotation(-M_PI_2);
_volumeSlider.value = [AVAudioSession sharedInstance].outputVolume;

_playBigButton.layer.borderWidth = 1.0;
_playBigButton.layer.borderColor = [UIColor whiteColor].CGColor;
Expand All @@ -54,6 +76,14 @@ - (void)viewDidLoad
[self autoHidePlayerBar];
}

- (void)dealloc
{
_volume = nil;

[_window removeFromSuperview], _window = nil;
[_mainWindow makeKeyAndVisible];
}

- (void)setPlayer:(AVPlayer *)player
{
@synchronized (self) {
Expand All @@ -69,7 +99,7 @@ - (void)setPlayer:(AVPlayer *)player

} else {

typeof(self) wself = self;
__weak typeof(self) wself = self;
timeObserver = [self.player addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(1.0 / 60.0, NSEC_PER_SEC)
queue:NULL
usingBlock:^(CMTime time){
Expand Down Expand Up @@ -141,7 +171,7 @@ - (void)hidePlayerBar
{
CGFloat height = _playerBarView.frame.size.height;

typeof(self) wself = self;
__weak typeof(self) wself = self;
[self setConstraintValue:height
forAttribute:NSLayoutAttributeBottom
duration:1.0 animations:^{
Expand All @@ -156,7 +186,7 @@ - (void)showPlayerBar
{
_playerBarView.hidden = NO;

typeof(self) wself = self;
__weak typeof(self) wself = self;
[self setConstraintValue:0
forAttribute:NSLayoutAttributeBottom
duration:0.5
Expand Down Expand Up @@ -219,6 +249,7 @@ - (void)didVolumeButtonSelected:(id)sender
{
_volumeSlider.alpha = 0.0;
_volumeSlider.hidden = NO;
_volumeSlider.value = [AVAudioSession sharedInstance].outputVolume;
[UIView animateWithDuration:0.25
animations:^{
_volumeSlider.alpha = 1.0;
Expand All @@ -241,31 +272,24 @@ - (void)didVolumeButtonSelected:(id)sender

- (void)didFullscreenButtonSelected:(id)sender
{
static BOOL navbar;
static BOOL statusbar;
static CGRect current_frame;
static UIView *container_view;
static UIWindow *mainWindow;
static UIViewController *original_parent;

UIViewController *parent = self.parentViewController; // AVPlayerViewController

if (mainWindow == nil)
mainWindow = [UIApplication sharedApplication].keyWindow;
if (_mainWindow == nil)
self.mainWindow = [UIApplication sharedApplication].keyWindow;

if (_window == nil)
{
statusbar = [UIApplication sharedApplication].isStatusBarHidden;
original_parent = parent.parentViewController;
current_frame = [parent.view convertRect:parent.view.frame toView:mainWindow];
container_view = parent.view.superview;
navbar = parent.navigationController.isNavigationBarHidden;
self.statusBarHidden = [UIApplication sharedApplication].isStatusBarHidden;
self.mainParent = parent.parentViewController;
self.currentFrame = [parent.view convertRect:parent.view.frame toView:_mainWindow];
self.containerView = parent.view.superview;
self.navbarHidden = parent.navigationController.isNavigationBarHidden;

[parent removeFromParentViewController];
[parent.view removeFromSuperview];
[parent willMoveToParentViewController:nil];

self.window = [[UIWindow alloc] initWithFrame:current_frame];
self.window = [[UIWindow alloc] initWithFrame:_currentFrame];
_window.backgroundColor = [UIColor blackColor];
_window.windowLevel = UIWindowLevelNormal;

Expand All @@ -280,7 +304,7 @@ - (void)didFullscreenButtonSelected:(id)sender
delay:0
options:UIViewKeyframeAnimationOptionLayoutSubviews
animations:^{
_window.frame = mainWindow.bounds;
_window.frame = _mainWindow.bounds;
} completion:^(BOOL finished) {
_fullscreenButton.transform = CGAffineTransformMakeScale(-1.0, -1.0);
_isFullscreen = YES;
Expand All @@ -297,28 +321,24 @@ - (void)didFullscreenButtonSelected:(id)sender
delay:0
options:UIViewKeyframeAnimationOptionLayoutSubviews
animations:^{
_window.frame = current_frame;
_window.frame = _currentFrame;
} completion:^(BOOL finished) {

_window.rootViewController = nil;
_fullscreenButton.transform = CGAffineTransformIdentity;

[original_parent addChildViewController:parent];
[container_view addSubview:parent.view];
[parent didMoveToParentViewController:original_parent];

[_window removeFromSuperview], self.window = nil;
[mainWindow makeKeyAndVisible];
[_mainParent addChildViewController:parent];
[_containerView addSubview:parent.view];
[parent didMoveToParentViewController:_mainParent];

container_view = nil;
original_parent = nil;
mainWindow = nil;
[_window removeFromSuperview], _window = nil;
[_mainWindow makeKeyAndVisible];

_isFullscreen = NO;
}];

[self.navigationController setNavigationBarHidden:navbar animated:YES];
[[UIApplication sharedApplication] setStatusBarHidden:statusbar withAnimation:UIStatusBarAnimationSlide];
[self.navigationController setNavigationBarHidden:_navbarHidden animated:YES];
[[UIApplication sharedApplication] setStatusBarHidden:_statusBarHidden withAnimation:UIStatusBarAnimationSlide];

}

Expand All @@ -341,7 +361,9 @@ - (void)didNormalScreenModeToParentViewController:(UIViewController*)parent

- (void)didVolumeSliderValueChanged:(id)sender
{
_player.volume = ((UISlider*)sender).value;
_mpSlider.value = ((UISlider*)sender).value;
[_mpSlider sendActionsForControlEvents:UIControlEventTouchUpInside];

[self autoHidePlayerBar];
}

Expand Down

0 comments on commit 952cdce

Please sign in to comment.