Skip to content

Commit

Permalink
Merge pull request #33 from nytm/develop
Browse files Browse the repository at this point in the history
v 0.5.3
  • Loading branch information
jaredsinclair committed Aug 22, 2016
2 parents 43db0d3 + 3ab5efe commit d4082e9
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 13 deletions.
2 changes: 1 addition & 1 deletion NYT360Video.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'NYT360Video'
s.version = '0.5.2'
s.version = '0.5.3'
s.summary = 'NYT360Video plays 360º video streamed from an AVPlayer.'

s.description = <<-DESC
Expand Down
2 changes: 1 addition & 1 deletion NYT360VideoExample/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>0.5.2</string>
<string>0.5.3</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
2 changes: 1 addition & 1 deletion NYT360VideoTests/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>0.5.2</string>
<string>0.5.3</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
2 changes: 1 addition & 1 deletion Sources/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>0.5.2</string>
<string>0.5.3</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
88 changes: 79 additions & 9 deletions Sources/NYT360PlayerScene.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,56 @@

#import "NYT360PlayerScene.h"

@interface NYT360PlayerScene ()
@class NYTSKVideoNode;

///-----------------------------------------------------------------------------
/// NYTSKVideoNodeDelegate
///-----------------------------------------------------------------------------

@protocol NYTSKVideoNodeDelegate <NSObject>

- (BOOL)videoNodeShouldAllowPlaybackToBegin:(NYTSKVideoNode *)videoNode;

@end

///-----------------------------------------------------------------------------
/// NYTSKVideoNode
///-----------------------------------------------------------------------------

/**
* There is a bug in SceneKit wherein a paused video node will begin playing again when the application becomes active. This is caused by cascading calls to `[fooNode setPaused:NO]` across all nodes in a scene. To prevent the video node from unpausing along with the rest of the nodes, we must subclass SKVideoNode and override `setPaused:`, only unpausing the node if `nytDelegate` allows it.
*/
@interface NYTSKVideoNode: SKVideoNode

/**
* The node's custom delegate. It's prefixed with `nyt_` to avoid any future conflicts with SKVideoNode properties, since this class may not be intended for subclassing.
*/
@property (nonatomic, weak) id<NYTSKVideoNodeDelegate> nyt_delegate;

@end

@implementation NYTSKVideoNode

- (void)setPaused:(BOOL)paused {
if (!paused && self.nyt_delegate != nil) {
if ([self.nyt_delegate videoNodeShouldAllowPlaybackToBegin:self]) {
[super setPaused:NO];
}
} else {
[super setPaused:paused];
}
}
@end

///-----------------------------------------------------------------------------
/// NYT360PlayerScene
///-----------------------------------------------------------------------------

@interface NYT360PlayerScene () <NYTSKVideoNodeDelegate>

@property (nonatomic, assign) BOOL videoPlaybackIsPaused;
@property (nonatomic, readonly) SCNNode *cameraNode;
@property (nonatomic, readonly) SKVideoNode *videoNode;
@property (nonatomic, readonly) NYTSKVideoNode *videoNode;

@end

Expand All @@ -22,32 +68,35 @@ @implementation NYT360PlayerScene
- (instancetype)initWithAVPlayer:(AVPlayer *)player boundToView:(SCNView *)view {
if ((self = [super init])) {

_videoPlaybackIsPaused = YES;

_camera = [SCNCamera new];

_cameraNode = ({
SCNNode *cameraNode = [SCNNode new];
cameraNode.camera = _camera;
cameraNode.position = SCNVector3Make(0, 0, 0);
cameraNode;
});
[self.rootNode addChildNode:_cameraNode];

SKScene *skScene = ({
SKScene *scene = [[SKScene alloc] initWithSize:CGSizeMake(1280, 1280)];
scene.shouldRasterize = YES;
scene.scaleMode = SKSceneScaleModeAspectFit;
_videoNode = ({
SKVideoNode *videoNode = [[SKVideoNode alloc] initWithAVPlayer:player];
NYTSKVideoNode *videoNode = [[NYTSKVideoNode alloc] initWithAVPlayer:player];
videoNode.position = CGPointMake(scene.size.width / 2, scene.size.height / 2);
videoNode.size = scene.size;
videoNode.yScale = -1;
videoNode.xScale = -1;
videoNode.nyt_delegate = self;
videoNode;
});
[scene addChild:_videoNode];
scene;
});

SCNNode *sphereNode = ({
SCNNode *sphereNode = [SCNNode new];
sphereNode.position = SCNVector3Make(0, 0, 0);
Expand All @@ -63,18 +112,39 @@ - (instancetype)initWithAVPlayer:(AVPlayer *)player boundToView:(SCNView *)view
view.scene = self;
view.pointOfView = self.cameraNode;
}

return self;
}

#pragma mark - Playback

- (void)play {
[self.videoNode play];

// See note in NYTSKVideoNode above.
self.videoPlaybackIsPaused = NO;

// Internally, SceneKit prefers to use `setPaused:` to toggle playback on a
// video node. Mimic this usage here to ensure consistency and avoid putting
// the player into an out-of-sync state.
self.videoNode.paused = NO;

}

- (void)pause {
[self.videoNode pause];

// See note in NYTSKVideoNode above.
self.videoPlaybackIsPaused = YES;

// Internally, SceneKit prefers to use `setPaused:` to toggle playback on a
// video node. Mimic this usage here to ensure consistency and avoid putting
// the player into an out-of-sync state.
self.videoNode.paused = YES;

}

- (BOOL)videoNodeShouldAllowPlaybackToBegin:(NYTSKVideoNode *)videoNode {
// See note in NYTSKVideoNode above.
return !self.videoPlaybackIsPaused;
}

@end
4 changes: 4 additions & 0 deletions Sources/NYT360ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ - (void)viewDidLoad {
self.view.backgroundColor = [UIColor blackColor];
self.view.opaque = YES;

// Prevent the edges of the "aspect-fill" resized player scene from being
// visible beyond the bounds of `self.view`.
self.view.clipsToBounds = YES;

// self.sceneView.showsStatistics = YES;
self.sceneView.autoresizingMask = UIViewAutoresizingNone;
self.sceneView.backgroundColor = [UIColor blackColor];
Expand Down

0 comments on commit d4082e9

Please sign in to comment.