22
33#import " ASVideoNode.h"
44
5- @interface ASVideoNode () {
5+ @interface ASVideoNode ()
6+ {
67 ASDN::RecursiveMutex _lock;
78
8- __weak id <ASVideoNodeDatasource> _datasource ;
9+ __weak id <ASVideoNodeDataSource> _dataSource ;
910
1011 BOOL _shouldBePlaying;
1112 AVAsset *_asset;
@@ -23,7 +24,8 @@ - (void)setInterfaceState:(ASInterfaceState)newState;
2324
2425@implementation ASVideoNode
2526
26- - (instancetype )init {
27+ - (instancetype )init
28+ {
2729 if (!(self = [super init ])) { return nil ; }
2830
2931 _playerNode = [[ASDisplayNode alloc ] initWithLayerBlock: ^CALayer *{
@@ -35,11 +37,14 @@ - (instancetype)init {
3537
3638 self.gravity = ASVideoGravityResizeAspect;
3739
38- [[NSNotificationCenter defaultCenter ] addObserver: self selector: @selector (restartVideo: ) name: AVPlayerItemDidPlayToEndTimeNotification object: nil ];
40+ [self addTarget: self action: @selector (tapped ) forControlEvents: ASControlNodeEventTouchUpInside];
41+
42+ [[NSNotificationCenter defaultCenter ] addObserver: self selector: @selector (didPlayToEnd: ) name: AVPlayerItemDidPlayToEndTimeNotification object: nil ];
3943
4044 return self;
4145}
4246
47+ // FIXME: Adopt interfaceStateDidChange API
4348- (void )setInterfaceState : (ASInterfaceState)newState
4449{
4550 [super setInterfaceState: newState];
@@ -51,9 +56,6 @@ - (void)setInterfaceState:(ASInterfaceState)newState
5156 if (_shouldBePlaying) {
5257 [self play ];
5358 }
54- if (_spinner) {
55- [self addSubnode: _spinner];
56- }
5759 }
5860}
5961
@@ -71,9 +73,9 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
7173 }
7274}
7375
74- - (void )restartVideo : (NSNotification *)notification
76+ - (void )didPlayToEnd : (NSNotification *)notification
7577{
76- if ( [[[ notification object ] asset ] isEqual: _asset] ) {
78+ if (ASObjectIsEqual ([[ notification object ] asset ], _asset) ) {
7779 [[((AVPlayerLayer *)_playerNode.layer) player ] seekToTime: CMTimeMakeWithSeconds (0 , 1 )];
7880
7981 if (_autorepeat) {
@@ -84,18 +86,12 @@ - (void)restartVideo:(NSNotification *)notification
8486 }
8587}
8688
87- - (void )layoutDidFinish
89+ - (void )layout
8890{
91+ [super layout ];
8992 _playerNode.frame = self.bounds ;
9093}
9194
92- - (void )didLoad {
93- [super didLoad ];
94-
95- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc ] initWithTarget: self action: @selector (tapped )];
96- [self .view addGestureRecognizer: tap];
97- }
98-
9995- (void )tapped
10096{
10197 if (_shouldBePlaying) {
@@ -134,7 +130,7 @@ - (void)fetchData
134130 }
135131
136132 }
137- if (_shouldAutoPlay ) {
133+ if (_shouldAutoplay ) {
138134 [self play ];
139135 }
140136}
@@ -189,7 +185,8 @@ - (AVAsset *)asset
189185 return _asset;
190186}
191187
192- - (void )setGravity : (ASVideoGravity)gravity {
188+ - (void )setGravity : (ASVideoGravity)gravity
189+ {
193190 ASDN::MutexLocker l (_lock);
194191
195192 switch (gravity) {
@@ -208,50 +205,56 @@ - (void)setGravity:(ASVideoGravity)gravity {
208205 }
209206}
210207
211- - (ASVideoGravity)gravity ;
208+ - (ASVideoGravity)gravity
212209{
213210 ASDN::MutexLocker l (_lock);
214211
215- if ([(( AVPlayerLayer *)_playerNode.layer).contentsGravity isEqualToString: AVLayerVideoGravityResize] ) {
212+ if (ASObjectIsEqual ((( AVPlayerLayer *)_playerNode.layer ).contentsGravity , AVLayerVideoGravityResize) ) {
216213 return ASVideoGravityResize;
217214 }
218- if ([(( AVPlayerLayer *)_playerNode.layer).contentsGravity isEqualToString: AVLayerVideoGravityResizeAspectFill] ) {
215+ if (ASObjectIsEqual ((( AVPlayerLayer *)_playerNode.layer ).contentsGravity , AVLayerVideoGravityResizeAspectFill) ) {
219216 return ASVideoGravityResizeAspectFill;
220217 }
221218
222219 return ASVideoGravityResizeAspect;
223220}
224221
225- - (void )play ;
222+ - (void )play
226223{
227224 ASDN::MutexLocker l (_lock);
228225
229- [[((AVPlayerLayer *)_playerNode.layer) player ] play ];
230- _shouldBePlaying = YES ;
231- _playButton.alpha = 0.0 ;
232- if ([self ready ] && ![self .subnodes containsObject: _spinner]) {
226+ if (!_spinner) {
233227 _spinner = [[ASDisplayNode alloc ] initWithViewBlock: ^UIView *{
234228 UIActivityIndicatorView *spinnnerView = [[UIActivityIndicatorView alloc ] initWithFrame: _playButton.frame];
235229 spinnnerView.color = [UIColor whiteColor ];
236230 [spinnnerView startAnimating ];
237231
238232 return spinnnerView;
239233 }];
240-
234+ }
235+
236+ if (![self ready ]) {
241237 [self addSubnode: _spinner];
242238 }
239+
240+ [[((AVPlayerLayer *)_playerNode.layer) player ] play ];
241+ _shouldBePlaying = YES ;
242+ _playButton.alpha = 0.0 ;
243+ // if ([self ready] && ![self.subnodes containsObject:_spinner]) {
244+ // }
243245}
244246
245- - (BOOL )ready ;
247+ - (BOOL )ready
246248{
247- return [((AVPlayerLayer *)_playerNode.layer) player ].currentItem .status ! = AVPlayerItemStatusReadyToPlay;
249+ return [((AVPlayerLayer *)_playerNode.layer) player ].currentItem .status = = AVPlayerItemStatusReadyToPlay;
248250}
249251
250- - (void )pause ;
252+ - (void )pause
251253{
252254 ASDN::MutexLocker l (_lock);
253255
254256 [[((AVPlayerLayer *)_playerNode.layer) player ] pause ];
257+ [((UIActivityIndicatorView *)_spinner.view) stopAnimating ];
255258 _shouldBePlaying = NO ;
256259 _playButton.alpha = 1.0 ;
257260}
@@ -261,6 +264,16 @@ - (AVPlayerItem *)currentItem
261264 return _currentItem;
262265}
263266
267+ - (ASDisplayNode *)spinner
268+ {
269+ return _spinner;
270+ }
271+
272+ - (AVPlayerItem *)curentItem
273+ {
274+ return _currentItem;
275+ }
276+
264277- (void )dealloc
265278{
266279 [[NSNotificationCenter defaultCenter ] removeObserver: self ];
0 commit comments