Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
KY1VSTAR committed Apr 15, 2019
1 parent 7b3a410 commit 4994534
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
1 change: 1 addition & 0 deletions Example/Example Swift/CustomPlayerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class CustomPlayerViewController: UIViewController {
func setupPictureInPicture() {
pipToggleButton.setImage(AVPictureInPictureController.pictureInPictureButtonStartImage(compatibleWith: nil), for: .normal)
pipToggleButton.setImage(AVPictureInPictureController.pictureInPictureButtonStopImage(compatibleWith: nil), for: .selected)
pipToggleButton.setImage(AVPictureInPictureController.pictureInPictureButtonStopImage(compatibleWith: nil), for: [.selected, .highlighted])

guard AVPictureInPictureController.isPictureInPictureSupported(),
let pictureInPictureController = AVPictureInPictureController(playerLayer: playerView.playerLayer) else {
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2018 ky1vstar <[email protected]>
Copyright (c) 2019 ky1vstar <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
34 changes: 19 additions & 15 deletions Source/Core/PiPPictureInPictureController.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#import "PiPPlayerViewController.h"
#import "PiPManager+Private.h"

static NSString *kPictureInPicturePossible = @"pictureInPicturePossible";
static __weak PiPPictureInPictureController *_currentPictureInPictureController;

@interface PiPPictureInPictureController () {
Expand All @@ -36,6 +35,8 @@ @interface PiPPictureInPictureController () {

@implementation PiPPictureInPictureController

#pragma mark - Class methods

+ (void)pictureInPictureControllerWillStart:(PiPPictureInPictureController *)pictureInPictureController {
if (_currentPictureInPictureController != pictureInPictureController) {
[_currentPictureInPictureController stopPictureInPicture];
Expand All @@ -50,6 +51,8 @@ + (void)pictureInPictureControllerWillStop:(PiPPictureInPictureController *)pict
}
}

#pragma mark - Initializers

- (instancetype)initWithPlayerLayer:(AVPlayerLayer *)playerLayer {
if (self = [super init]) {
_playerLayer = playerLayer;
Expand Down Expand Up @@ -84,6 +87,8 @@ - (void)dealloc {
[self stopPictureInPicture];
}

#pragma mark - AVPictureInPictureController methods

- (void)setDelegate:(id<AVPictureInPictureControllerDelegate>)delegate {
_delegate = delegate;

Expand All @@ -110,12 +115,8 @@ - (void)stopPictureInPicture {
[_currentPlayerViewController stop];
}

- (void)updatePossibility {
BOOL newValue = _playerLayerObserver.valid && _allowsPictureInPicturePlayback && PiPManager.pictureInPicturePossible;

if (newValue != _pictureInPicturePossible) {
self.pictureInPicturePossible = newValue;
}
- (void)stopPictureInPictureEvenWhenInBackground {
[self stopPictureInPicture];
}

- (void)setAllowsPictureInPicturePlayback:(BOOL)allowsPictureInPicturePlayback {
Expand All @@ -124,7 +125,17 @@ - (void)setAllowsPictureInPicturePlayback:(BOOL)allowsPictureInPicturePlayback {
[self updatePossibility];
}

#pragma mark - Mimic AVPictureInPictureController
#pragma mark - Possibility

- (void)updatePossibility {
BOOL newValue = _playerLayerObserver.valid && _allowsPictureInPicturePlayback && PiPManager.pictureInPicturePossible;

if (newValue != _pictureInPicturePossible) {
self.pictureInPicturePossible = newValue;
}
}

#pragma mark - Runtime support for non-implemented methods

- (BOOL)isKindOfClass:(Class)aClass {
if (aClass == AVPictureInPictureController.class) {
Expand Down Expand Up @@ -156,13 +167,6 @@ - (void)forwardInvocation:(NSInvocation *)anInvocation {
[anInvocation invoke];
}

//- (BOOL)pictureInPictureWasStartedWhenEnteringBackground {
// return NO;
//}
//
//- (void)playerLayerLayoutDidChange {
//}

#pragma mark - PiPPlayerViewControllerDelegate

- (void)playerViewControllerWillStartPictureInPicture:(PiPPlayerViewController *)playerViewController {
Expand Down
14 changes: 12 additions & 2 deletions Source/Core/PiPPlayerLayerObserver.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "PiPPlayerLayerObserver.h"
#import "NSObject+PiPhone.h"

static BOOL kAlwaysReadyForDisplay = NO;
static NSString *kPlayerKeyPath = @"player";
static NSString *kCurrentItemKeyPath = @"player.currentItem";
static NSString *kReadyForDisplayKeyPath = @"readyForDisplay";
Expand All @@ -33,6 +34,15 @@ @interface PiPPlayerLayerObserver ()

@implementation PiPPlayerLayerObserver

+ (void)initialize {
// AVQueuePlayer is broken on iOS 9. After currentItem being changed AVPlayerLayer's readyForDisplay will always remain NO;
if (@available(iOS 10.0, *)) {
kAlwaysReadyForDisplay = NO;
} else {
kAlwaysReadyForDisplay = YES;
}
}

- (instancetype)initWithPlayerLayer:(AVPlayerLayer *)playerLayer {
if (self = [super init]) {
_delegates = [NSHashTable weakObjectsHashTable];
Expand Down Expand Up @@ -63,7 +73,7 @@ - (void)setupObservations {
[self addObserversForPlayer:_player];

_currentPlayerItem = _player.currentItem;
_readyForDisplay = _playerLayer.readyForDisplay;
_readyForDisplay = _playerLayer.readyForDisplay || kAlwaysReadyForDisplay;
_playerItemStatus = playerItem.status;
_presentationSize = playerItem.presentationSize;
_playing = _player.rate > 0;
Expand Down Expand Up @@ -139,7 +149,7 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N

} else if ([keyPath isEqualToString:kReadyForDisplayKeyPath]) {
NSNumber *number = [change[NSKeyValueChangeNewKey] ifNullThenNil];
_readyForDisplay = number.boolValue;
_readyForDisplay = number.boolValue || kAlwaysReadyForDisplay;

[self updateValidity];
[self updateInitializing];
Expand Down

0 comments on commit 4994534

Please sign in to comment.